Hyp*_*837 2 python asynchronous scrapy web-scraping
我最近在我的项目中运行了一个蜘蛛,但我觉得它就像是在等待一页完成后再移动到另一页。如果我在scrapy的性质上是正确的,它会移动到另一个页面,直到收到前一个响应。在向下滚动后的这个页面中,我看到async defused 这意味着通过添加该方法显式地使该方法异步。如果我不放入async-await我的蜘蛛,它们会不会变得异步。他们会等到收到回复吗?如果我有任何误解,请让我知道,并提前致谢。
是的,因为@Gallaecio 说scrapy 默认是异步的。我想补充一点,我可以使代码的同步部分异步。这样,
同步请求
def parse(self, response, current_page):
url = 'https://example.com/search?page={}'
# do some stuff
self.current_page += 1
yield Request(url.format(current_page), call_back=self.parse)
Run Code Online (Sandbox Code Playgroud)
异步请求
def parse(self, response):
url = 'https://example.com/something?page={}'
# do some stuff
for page in range(self.total_pages): # variables are self explainable
yield Requests(url.format(page), callback=self.parse)
Run Code Online (Sandbox Code Playgroud)