首页 > 解决方案 > 如何使用 Scrapy 获取亚马逊搜索的所有结果?

问题描述

我正在尝试从亚马逊上抓取有关衬衫的信息。我的蜘蛛目前接受一个关键字列表并使用它们在亚马逊上执行搜索。对于每个搜索页面,我调用 parse 函数。我想抓取每个生成的项目并使用scrapy的“reponse.follow(...)”方法进一步检查它们。

我目前正在尝试使用“response.css('.s-result-item')”来获得所有结果。我也尝试过使用“response.css('.sg-col-inner')。无论哪种方式,它都会得到一些结果,但不是全部,有时每页只会得到两三个结果。如果我添加.extract() 到它完全失败的语句。这是我的解析方法:

def parse(self, response):
    print("========== starting parse ===========")
    print(response.text)
    all_containers = response.css(".s-result-item")
    for shirts in all_containers:
        next_page = shirts.css('.a-link-normal::attr(href)').extract_first()
        if next_page is not None:
            if "https://www.amazon.com" not in next_page:
                next_page = "https://www.amazon.com" + next_page
            yield response.follow('http://api.scraperapi.com/?api_key=mykey&url=' + next_page, callback=self.parse_dir_contents)

    second_page = response.css('li.a-last a::attr(href)').get()
    if second_page is not None and AmazonSpiderSpider.page_number < 3:
        AmazonSpiderSpider.page_number += 1
        yield response.follow('http://api.scraperapi.com/?api_key=mykey&url='+ second_page, callback=self.parse)
    else:
        AmazonSpiderSpider.current_keyword = AmazonSpiderSpider.current_keyword + 1

我是 Python 和 Scrapy 的新手,我不知道我是否应该使用 reponse.follow 或 scrapy.Request,或者这是否会有所作为。有任何想法吗?

标签: pythoncssscrapyamazon

解决方案


我已经使用:

对于 response.css("h2.a-size-mini a").xpath("@href").extract() 中的 next_page:


推荐阅读