【问题标题】:Scrapy not finding table cssScrapy找不到表格css
【发布时间】:2017-03-11 04:15:57
【问题描述】:

最近刚开始使用 Scrapy,到目前为止,我一直很幸运,直到这个问题。我似乎无法在这里“找到”积分榜;

http://www.baseball-reference.com/leagues/MLB/2016-standings.shtml#all_expanded_standings_overall

它的 id = '#expanded_standings_overall' 但我无法在我的蜘蛛或 shell 中找到它。我能够得到 #all_expanded_standings_overall 的结果,因为有一个具有该 ID 的 div。在 shell 中提取它向我展示了我想要的表,但即使在其中我也无法使用 'tbody' 或 'tr' 或我尝试过的其他任何东西找到它。

【问题讨论】:

  • 您能否发布您的尝试,以便我们查看您哪里出错了?
  • @Hannah,我什至不知道该给你看什么?如果我做scrapy shell thatsite.com然后输入response.css('#expanded_standings_overall')返回[]。我完全迷失了为什么它找不到那个ID?这与我已经在该域中找到其他几个类似表的方式相同。

标签: css scrapy


【解决方案1】:

如果您查看页面源代码,您会看到有问题的 id (expanded_standings_overall)

<div class="placeholder"></div>
<!--
    <div class="table_outer_container">
        <div class="overthrow table_container" id="div_expanded_standings_overall">
            <table class="sortable stats_table" id="expanded_standings_overall" data-cols-to-freeze=2>
                <caption>MLB Detailed Standings</caption>
                    ... sweet data here ..
                </table>
        </div>
    </div>
-->
</div>

HTML cmets 似乎是一种将内容隐藏到我们无辜的抓取工具的技巧;)

有趣的是,Firebug 不显示此 cmets ...?

解决该问题的一种方法是提取 cmets,将其删除,然后继续处理 cmets 中的数据。例如:

$ scrapy shell www.baseball-reference.com/leagues/MLB/2016-standings.shtml
>>> view(response)
>>> from scrapy.selector import Selector
>>> sel = Selector(response)
>>> sel.xpath('//table[@id="expanded_standings_overall"]')
[]
>>> import re
>>> regex = re.compile(r'<!--(.*)-->', re.DOTALL)
>>> for comment in sel.xpath('//comment()').re(regex):
>>>     table = Selector(text=comment).xpath('//table[@id="expanded_standings_overall"]')
>>>     print(table)
...
[]
[]
[<Selector xpath='//table[@id="expanded_standings_overall"]' data='<table class="sortable stats_table" id="'>]
[]
[]

如您所见,我更喜欢 XPATH 选择器而不是 CSS,但它们原则上是相同的,请参阅 https://doc.scrapy.org/en/latest/topics/selectors.html

【讨论】:

  • 哈!厚脸皮的混蛋,谢谢你的提示。我迷路了。
  • @JordanWayneCrabb 如果它解决了您的问题,也许您想将答案标记为已接受?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多