我正在亚马逊畅销书页面上测试 scrapy 蜘蛛(请参见下面的 URL),但它返回奇怪的价格数字或根本没有输出,正如您在最后的输出中看到的那样(我只共享了 1 页的输出) 。css 选择器可能有问题,但我不确定。我希望蜘蛛将输出保存在 JSON 文件中,这样我就可以快速将其转换为 pandas 数据帧进行一些分析。这是我在终端中编写的用于运行蜘蛛的代码: scrapycrawl amazon_booksUK -o somefilename.json我知道要查看的内容有很多,但如果您有时间,这确实会对我有帮助!:)url = 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=3890 22011 &ref=sr_pg_11.蜘蛛代码:import scrapyfrom ..items import AmazonscrapeItemclass 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)
1 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
你只需要像这样使用用户代理:
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
就像真正的浏览器使用的东西
添加回答
举报
0/150
提交
取消