测试搜寻器:
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'quotes-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
然后我写了一个main.py:
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
execute(["scrapy","crawl","quotes"])
并且我添加了python scrapy调试器配置,一切都很好,直到被击中 产生scrapy.Request(url = url,callback = self.parse) 它不会进入回调解析功能?
答案 0 :(得分:1)
好的,现在我知道为什么了,因为yield请求是异步的,并且回调将在子线程返回结果后调用,所以稍等片刻,它将最终调试到解析函数中
答案 1 :(得分:0)
您确定您的parse
函数在类内部吗?该代码段看起来您的缩进错误。