【发布时间】:2021-06-24 17:58:21
【问题描述】:
我正在尝试 scrape 这个website 用于学术目的,使用 css/xpath 选择器进行scrapy。
我需要在 ID 为DataTables_Table_0 的表格中选择td 中的详细信息。但是我什至无法选择包含表格的 div 元素,更不用说表格数据了。
我要解析的 HTML 块是
# please ignore wrong indentation
<div id="fund-selector-data">
<div class=" ">
<div id="DataTables_Table_0_wrapper" class="dataTables_wrapper no-footer">
<div class="dataTables_scroll">
<div class="dataTables_scrollHead"
</div>
<div class="dataTables_scrollBody" style="position: relative; overflow: auto; width: 100%;">
<table class="row-border dataTable table-snapshot no-footer" data-order="[]" cellspacing="0" width="100%"
id="DataTables_Table_0" role="grid" style="width: 100%;">
<thead>
</thead>
<tbody>
<tr role="row" class="odd">
<td><a href="/downloads/fund-card/38821" class="orange">PDF</a></td>
<td class=" text-left"><a href="/funds/38821/aditya-birla-sun-life-bal-bhavishya-yojna-direct-plan">ABSL Bal
Bhavishya Yojna Dir</a> | <a class="invest-online-blink invest-online " target="_blank"
href="/funds/invest-online-tracking/420/" data-amc="aditya-birla-sun-life-mutual-fund"
data-fund="aditya-birla-sun-life-bal-bhavishya-yojna-direct-plan">Invest Online</a></td>
<td data-order="" class=" text-left">
<div class="raterater-layer text-left test-fund-rating-star "><small>Unrated</small></div>
</td>
<td class=" text-left"><a
href="/premium/?utm_medium=vro&utm_campaign=premium-unlock&utm_source=fund-selector">
<div class="unlock-premium"></div>
</a></td>
</tbody>
scrapy CSS 选择器如下:
# Selecting Table (selector)
response.css("#DataTables_Table_0") # returns blank list
# Selecting div class (selector)
response.css(".dataTables_scrollBody") # returns blank list
# Selecting td element
response.css("#DataTables_Table_0 tbody tr td a::text").getall() # returns blank list
我也尝试过 xpath 来选择元素,但得到了相同的结果。我发现我无法选择div 下方的任何元素,并且类为空。我无法理解为什么它在这种情况下不起作用?我错过了什么吗?任何帮助将不胜感激。
【问题讨论】:
标签: python web-scraping xpath scrapy css-selectors