对于一个项目,我运行了大量的垃圾请求的某些搜索词。这些请求使用相同的搜索词,但时间范围不同,如下面URL中的日期所示。在
尽管URL引用的日期和页面不同,但我收到的值与所有请求的输出值相同。它看起来像是脚本正在获取第一个值,并将相同的输出分配给所有后续请求。在
import scrapy
class QuotesSpider(scrapy.Spider):
name = 'quotes'
allowed_domains = ['google.com']
start_urls = ['https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2004%2Ccd_max%3A12%2F31%2F2004&tbm=nws',
'https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2005%2Ccd_max%3A12%2F31%2F2005&tbm=nws',
'https://www.google.com/search?q=Activision&biw=1280&bih=607&source=lnt&tbs=cdr%3A1%2Ccd_min%3A01%2F01%2F2006%2Ccd_max%3A12%2F31%2F2006&tbm=nws',
]
def parse(self, response):
item = {
'search_title': response.css('input#sbhost::attr(value)').get(),
'results': response.css('#resultStats::text').get(),
'url': response.url,
}
yield item
我找到一条线discussing a similar problem with BeautifulSoup。解决方案是在脚本中添加头,从而使其使用浏览器作为用户代理:
^{pr2}$在scrayseems to be different though中应用头的方法。有人知道如何将它最好地包含在Scrapy中吗,尤其是对同时包含多个url的start_urls
的引用?在
你不需要在这里修改标题。您需要设置Scrapy允许您直接执行的用户代理。在
现在您将得到如下输出:
^{pr2}$根据Scrapy 1.7.3 document。你的头不会像其他人一样通用。它应该是相同的网站,你正在刮。您将从控制台网络选项卡了解标题。在
像下面这样添加它们并打印响应。在
相关问题 更多 >
编程相关推荐