如何使用Scrapy获取amazon搜索的所有结果?

2024-09-28 05:38:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从亚马逊网站上搜集衬衫的信息。我的蜘蛛目前接受关键字列表,并使用它们在Amazon上执行搜索。对于每个搜索页,我都调用parse函数。我想抓住每一个产生的项目,并进一步检查他们使用刮擦的“响应。跟随(…)”方法。你知道吗

我目前正在尝试使用“响应.css('.s-result-item')”以获取所有结果。我也试过使用“响应.css('.sg col内部')。不管怎样,它都会得到一些结果,但不是全部,有时每页只能得到两三个。如果我在语句中添加.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的新手,我不知道我是否应该使用它响应。跟随或者皮屑。请求,或者这是否会产生影响。有什么想法吗?你知道吗


Tags: 方法selfcomapiifparseresponsepage
1条回答
网友
1楼 · 发布于 2024-09-28 05:38:48

我通过以下方法完成了这项工作:

中的下一页响应.css(“h2.a-size-mini a”).xpath(“@href”).extract():

相关问题 更多 >

    热门问题