python - 如何使用 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,或者这是否会有所作为。有任何想法吗?
解决方案
我已经使用:
对于 response.css("h2.a-size-mini a").xpath("@href").extract() 中的 next_page:
推荐阅读
- angular - 我们可以在 TestBed 中配置 Injector
- java - Hamcrest:如何在 Collections 中使用一个/或可组合的匹配器
- c++ - 在 NTL 中构造多项式的标准方法是什么?
- php - metaphone() 中的音素参数有什么作用?
- java - java中tomcat下如何用jsp获取不同的会话?
- jsf-2 - ui:repeat 不起作用,DOM 中也没有
- python-3.x - python3 雅虎收益日历
- python - 从 localhost 访问 AWS 中的 docker 容器
- .net - 在 Heroku 上托管 .NET Core 控制台应用程序
- node.js - 使用 cookie-parser 销毁 http-only cookie