查看Twitter:www.twitter.com/twitter
您会看到关注者数量显示为5790万,但是如果将鼠标悬停在该值上,您将看到确切的关注者数量。
它在源中显示为:
<span class="ProfileNav-value" data-count="57939946" data-is-compact="true">57.9M</span>
当我在Chrome上检查此跨度时,我使用:
(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[3]
我正在尝试使用上述方法仅提取属性“ data-count”:
def parseTwitter(self, response):
company_name=response.meta['company_name']
l=ItemLoader(item=TwitterItem(), response=response)
l.add_value('company_name', company_name)
l.add_xpath('twitter_tweets', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[1]/text()")
l.add_xpath('twitter_following', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[2]/text()")
l.add_xpath('twitter_followers', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[3]/text()")
...但是我什么也没回来:
2018-10-18 10:22:07 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-10-18 10:22:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://twitter.com/ADP> (referer: None)
2018-10-18 10:22:12 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://twitter.com/Workday> (referer: None)
2018-10-18 10:22:16 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://twitter.com/OracleHCM> (referer: None)
2018-10-18 10:22:16 [scrapy.core.engine] INFO: Closing spider (finished)
2018-10-18 10:22:16 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 892,
'downloader/request_count': 3,
'downloader/request_method_count/GET': 3,
'downloader/response_bytes': 199199,
'downloader/response_count': 3,
'downloader/response_status_count/200': 3,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 10, 18, 10, 22, 16, 833691),
'log_count/DEBUG': 4,
'log_count/INFO': 7,
'memusage/max': 52334592,
'memusage/startup': 52334592,
'response_received_count': 3,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2018, 10, 18, 10, 22, 7, 269320)}
解决方案: 根据下面的pwinz建议,我试图从属性中提取文本值“ .text()”,其中@ -ing属性应该为您提供值。我最后的-可行的-解决方案是:
def parseTwitter(self, response):
company_name=response.meta['company_name']
print('### ### ### Inside PARSE TWITTER ### ### ###')
l=ItemLoader(item=TwitterItem(), response=response)
l.add_value('company_name', company_name)
l.add_xpath('twitter_tweets', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[1]")
l.add_xpath('twitter_following', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[2]")
l.add_xpath('twitter_followers', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav-value']/@data-count)[3]")
yield l.load_item()
答案 0 :(得分:1)
这是因为数据是用Javascript处理的,但是Scrapy仅下载HTML,而不执行任何JS / AJAX代码。
使用Scrapy进行抓取时,请始终在浏览器中禁用Javascript,然后找到要抓取的内容,如果可用,请使用选择器/ xpath,否则,请检查网页上的JS / AJAX调用以了解其如何加载数据
所以,要刮擦关注者的人数
您可以使用以下CSS选择器
--net=host
草率代码
.ProfileNav-item.ProfileNav-item--followers a
答案 1 :(得分:0)
关于其他答案,动态内容不是这里的问题。您正在尝试从text()
属性中获取data-count
。您应该只能够从@data-count
获取数据。
尝试以下模式:
l.add_xpath('twitter_tweets', "(//ul[@class='ProfileNav-list']/li/a/span[@class='ProfileNav
-value']/@data-count)[1]")
对我有用。