我正在亚马逊畅销书页面上测试 scrapy 蜘蛛(请参见下面的 URL),但它返回奇怪的价格数字或根本没有输出,正如您在最后的输出中看到的那样(我只共享了 1 页的输出) 。css 选择器可能有问题,但我不确定。我希望蜘蛛将输出保存在 JSON 文件中,这样我就可以快速将其转换为 pandas 数据帧进行一些分析。这是我在终端中编写的用于运行蜘蛛的代码: scrapycrawl amazon_booksUK -o somefilename.json
我知道要查看的内容有很多,但如果您有时间,这确实会对我有帮助!:)
1.蜘蛛代码:
import scrapy
from ..items import AmazonscrapeItem
class AmazonSpiderSpider(scrapy.Spider):
page_number = 2
name = 'amazon_booksUK'
start_urls = [
'https://www.amazon.co.uk/s?i=stripbooks&bbn=266239&rh=n%3A266239%2Cp_72%3A184315031%2Cp_36%3A389028011&dc&page=1&fst=as%3Aoff&qid=1598942460&rnid=389022011&ref=sr_pg_1'
]
def parse(self, response):
items = AmazonscrapeItem()
# if multiple classes --> .css("::text").extract()
product_name = response.css('.a-color-base.a-text-normal::text').extract()
product_author = response.css('.a-color-secondary .a-size-base.a-link-normal').css('::text').extract()
product_nbr_reviews = response.css('.a-size-small .a-link-normal .a-size-base').css('::text').extract()
product_type = response.css('.a-spacing-top-small .a-link-normal.a-text-bold').css('::text').extract()
product_price = response.css('.a-spacing-top-small .a-price-whole').css('::text').extract()
product_more_choice = response.css('.a-spacing-top-mini .a-color-secondary .a-link-normal').css('::text').extract()
# this only selects the element that has the image --> need stuff inside src (source attr)
胡子哥哥
相关分类