测试爬虫:
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调试器配置,一切都很好,直到命中yield scrapy.Request(url = url,callback = self.parse)它没有进入回调解析函数?
好的,现在我知道为什么,因为yield请求是异步的,并且在子线程返回结果后将调用回调,所以稍等一下,它最终将调试到解析函数中
你确定你的parse
功能在课堂内吗?此代码段看起来像是有错误的缩进。