Python 3.x response.body正在使用python中的scrapy返回一个空文件
我正在尝试使用python中的scrapy制作一个网络爬虫,它可以在您进行搜索时提取google在右侧显示的信息 我使用的URL是: 在另一个问题中,我问了相同的(),有人建议我将response.body写入一个文件,但我得到的是一个空文件,当我尝试另一个URL时,这没有发生 这是我的代码:Python 3.x response.body正在使用python中的scrapy返回一个空文件,python-3.x,scrapy,web-crawler,Python 3.x,Scrapy,Web Crawler,我正在尝试使用python中的scrapy制作一个网络爬虫,它可以在您进行搜索时提取google在右侧显示的信息 我使用的URL是: 在另一个问题中,我问了相同的(),有人建议我将response.body写入一个文件,但我得到的是一个空文件,当我尝试另一个URL时,这没有发生 这是我的代码: import scrapy class google1(scrapy.Spider): name = 'google1' def start_requests(self):
import scrapy
class google1(scrapy.Spider):
name = 'google1'
def start_requests(self):
urls = ['http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
'https://www.google.com/search?q=la%20cuarta',
'https://docs.scrapy.org/en/latest/intro/tutorial.html']
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'page-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
它甚至没有从google搜索中写入文件,但在scrapy shell response.body中不是空的好的,我测试了你的代码,它工作了,即爬行器下载所有页面,包括googel页面。 您的问题可能在设置中,请将以下内容添加到您的设置中:
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
ROBOTSTXT_OBEY = False
好的,我测试了你的代码,它工作了,也就是说蜘蛛下载所有的页面,包括googel页面。 您的问题可能在设置中,请将以下内容添加到您的设置中:
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
ROBOTSTXT_OBEY = False