我正在尝试清除此网站上的所有产品: https://www.jny.com/collections/jackets
它将获得所有产品的链接,然后逐个将它们刮去。我正在尝试通过多线程加速这个过程。代码如下:
def yield1(self, url):
print("inside function")
yield scrapy.Request(url, callback=self.parse_product)
def parse(self, response):
print("in herre")
self.product_url = response.xpath('//div[@class = "collection-grid js-filter-grid"]//a/@href').getall()
print(self.product_url)
for pu in self.product_url:
print("inside the loop")
with ThreadPoolExecutor(max_workers=10) as executor:
print("inside thread")
executor.map(self.yield1, response.urljoin(pu))
它应该创建一个包含10个线程的池,每个线程将在URL列表上执行yield1()。问题是没有调用yield1()方法。你知道吗
yield1
是一个生成函数。为了让它产生一个值,你必须调用next。 将其更改为返回值你知道吗警告:我不知道我真的不太懂刮痧。你知道吗
Overview in the docs表示请求是异步发出的。你的代码看起来不像那些文档中给出的例子。概述中的示例显示了使用
response.follow
在parse
方法中发出的后续请求。您的代码看起来像是试图从页面中提取链接,然后异步地刮取这些链接,并使用不同的方法对其进行解析。因为看起来Scrapy会为您做这件事并处理异步性(?)我认为您只需要在spider中定义另一个解析方法,并使用response.follow
来调度更多的异步请求。你不应该需要期货,the new requests should all be processed asynchrounously。你知道吗我没有办法测试这个,但我认为你的蜘蛛应该看起来更像这样:
这假设
self.product_url = response.xpath('//div[@class = "collection-grid js-filter-grid"]//a/@href').getall()
做了它应该做的事情。你知道吗甚至可能有一个单独的蜘蛛来解析后续链接。或者使用CrawlSpider。你知道吗
相关SO问答
Scraping links with Scrapy
scraping web page containing anchor tag using scrapy
Use scrapy to get list of urls, and then scrape content inside those urls(看起来很熟悉)
Scrapy, scrape pages from second set of links
many more
相关问题 更多 >
编程相关推荐