使用ID的css选择器在scrapy中不起作用

Posted

技术标签:

【中文标题】使用ID的css选择器在scrapy中不起作用【英文标题】:css selector using ID not working in scrapy 【发布时间】:2018-09-25 08:35:19 【问题描述】:

我正在从这个页面抓取“共同财富游戏”的奖牌数量:https://en.wikipedia.org/wiki/1930_British_Empire_Games

一旦数据被抓取,我想移动到下一页。为此,我想选择一个具有attribute ID '#collapsibleTable1'<table> 标签。

现在有趣的部分来了。当我在 chrome 控制台上执行 $('#collapsibleTable1') 时,我得到了所需的输出。

但是,当我尝试在 scrapy shell 中执行 response.css('#collapsibleTable1') 时,它给出了一个空列表。

如果有人能解释为什么它会这样,那将是非常有帮助的。

【问题讨论】:

【参考方案1】:

似乎发生了一些 javascript 操作,因为该 id 不包含在实际的 html 源代码中(如果你 print(response.text) 可以看到)

Chrome 的开发工具会在所有 javascript 执行完毕后显示 DOM 的当前状态,这不是 scrapy 看到的。

看源码,你要的数据显示为:

<table class="nowraplinks collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit">
<tr>
<th scope="col" class="navbox-title" colspan="2">
<div class="plainlinks hlist navbar mini">
<ul>
<li class="nv-view"><a href="/wiki/Template:Commonwealth_Games_Medal_Counts" title="Template:Commonwealth Games Medal Counts"><abbr title="View this template" style=";;background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;">v</abbr></a></li>
<li class="nv-talk"><a href="/wiki/Template_talk:Commonwealth_Games_Medal_Counts" title="Template talk:Commonwealth Games Medal Counts"><abbr title="Discuss this template" style=";;background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;">t</abbr></a></li>
<li class="nv-edit"><a class="external text" href="//en.wikipedia.org/w/index.php?title=Template:Commonwealth_Games_Medal_Counts&amp;action=edit"><abbr title="Edit this template" style=";;background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;">e</abbr></a></li>
</ul>
</div>
<div id="Commonwealth_Games_medal_tables" style="font-size:114%;margin:0 4em"><a href="/wiki/All-time_Commonwealth_Games_medal_table" title="All-time Commonwealth Games medal table">Commonwealth Games medal tables</a></div>
</th>
</tr>
<tr>
<td colspan="2" class="navbox-list navbox-odd hlist" style="width:100%;padding:0px">
<div style="padding:0em 0.25em">
<ul>
<li><a href="/wiki/1930_British_Empire_Games#Medal_table" title="1930 British Empire Games">1930</a></li>
<li><a href="/wiki/1934_British_Empire_Games#Medals_by_country" title="1934 British Empire Games">1934</a></li>
<li><a href="/wiki/1938_British_Empire_Games#Medals_by_country" title="1938 British Empire Games">1938</a></li>
<li><a href="/wiki/1950_British_Empire_Games#Medals_by_country" title="1950 British Empire Games">1950</a></li>
<li><a href="/wiki/1954_British_Empire_and_Commonwealth_Games#Medal_table" title="1954 British Empire and Commonwealth Games">1954</a></li>
<li><a href="/wiki/1958_British_Empire_and_Commonwealth_Games#Medals_by_country" title="1958 British Empire and Commonwealth Games">1958</a></li>
<li><a href="/wiki/1962_British_Empire_and_Commonwealth_Games#Medals_by_country" title="1962 British Empire and Commonwealth Games">1962</a></li>
<li><a href="/wiki/1966_British_Empire_and_Commonwealth_Games#Medals_by_country" title="1966 British Empire and Commonwealth Games">1966</a></li>
<li><a href="/wiki/1970_British_Commonwealth_Games#Medals_by_country" title="1970 British Commonwealth Games">1970</a></li>
<li><a href="/wiki/1974_British_Commonwealth_Games#Medals_by_country" title="1974 British Commonwealth Games">1974</a></li>
<li><a href="/wiki/1978_Commonwealth_Games#Medals_by_country" title="1978 Commonwealth Games">1978</a></li>
<li><a href="/wiki/1982_Commonwealth_Games#Medals_by_country" title="1982 Commonwealth Games">1982</a></li>
<li><a href="/wiki/1986_Commonwealth_Games#Medals_by_country" title="1986 Commonwealth Games">1986</a></li>
<li><a href="/wiki/1990_Commonwealth_Games#Medals_by_country" title="1990 Commonwealth Games">1990</a></li>
<li><a href="/wiki/1994_Commonwealth_Games#Medal_table" title="1994 Commonwealth Games">1994</a></li>
<li><a href="/wiki/1998_Commonwealth_Games#Medal_table" title="1998 Commonwealth Games">1998</a></li>
<li><a href="/wiki/2002_Commonwealth_Games#Final_medal_table" title="2002 Commonwealth Games">2002</a></li>
<li><a href="/wiki/2006_Commonwealth_Games_medal_table" title="2006 Commonwealth Games medal table">2006</a></li>
<li><a href="/wiki/2010_Commonwealth_Games_medal_table" title="2010 Commonwealth Games medal table">2010</a></li>
<li><a href="/wiki/2014_Commonwealth_Games_medal_table" title="2014 Commonwealth Games medal table">2014</a></li>
<li><a href="/wiki/2018_Commonwealth_Games_medal_table" title="2018 Commonwealth Games medal table">2018</a></li>
</ul>
</div>
</td>
</tr>
</table>

【讨论】:

【参考方案2】:

我有同样的问题,刚开始爬网,发现我无法从网站上爬取某些内容。正如stranac所说,有些内容是由javascript动态渲染的,我们需要去数据源解决。

添加我的答案,因为像我这样的一些人不知道如何开始,可能需要一些指导, 如何从数据源获取数据请看scrapy的官方文档,根据你的情况有多种处理方式。

如果数据是在 Javascript 代码中定义的 - 使用 wgrep 查找数据源的 URL 如果数据来自原始 URL - 需要检查源代码并查看它们在哪里传递 如果数据是用 Javascript 硬编码的 - 我们需要解析 Javascript 并从那里获取数据

从上面我的理解是,有两种方法可以处理这个问题:

使用 scrapy-splash 以便您能够检索网页 DOM 的 HTML,然后您的 css 选择器将起作用 使用 selenium 无头浏览器,在动态网站中非常流行,基本上程序是从浏览器中获取您所看到的。

更多细节在官方文档中介绍。希望参考有所帮助。

【讨论】:

以上是关于使用ID的css选择器在scrapy中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 jQuery :not() 选择器在 CSS 中不起作用?

类和 id 选择器在 Antenna House Formatter 中不起作用

jQuery 属性选择器在 Internet Explorer 中不起作用

jquery图像选择器在IE7中不起作用

:visible 选择器在 Internet Explorer 中不起作用

第 n 个子选择器在反应地图渲染功能中不起作用