python - 保存 Scrapy 'start_urls' 并正确存储在数据框中
问题描述
我正在使用 Scrapy 来抓取一些网站数据。但我无法正确获取我的数据。
这是我的代码的输出(见下面的代码):
在命令行中:
scrapy crawl myspider -o items.csv
输出:
asin_product product_name
ProductA,,,ProductB,,,ProductC,,, BrandA,,,BrandB,,,BrandC,,,
ProductA,,,ProductD,,,ProductE,,, BrandA,,,BrandB,,,BrandA,,,
#Note that the rows are representing the start_urls and that the ',,,'
#three commas are separating the data.
期望的输出:
scrapy crawl myspider -o items.csv
Start_URL asin_product product_name
URL1 ProductA BrandA
URL1 ProductB BrandB
URL1 ProductC BrandC
URL2 ProductA BrandA
URL2 ProductD BrandB
URL2 ProductE BrandA
我在 Scrapy 中使用的代码:
import scrapy
from amazon.items import AmazonItem
class AmazonProductSpider(scrapy.Spider):
name = "AmazonDeals"
allowed_domains = ["amazon.com"]
#Use working product URL below
start_urls = [
"https://www.amazon.com/s?k=shoes&ref=nb_sb_noss_2", # This should
be #URL 1
"https://www.amazon.com/s?k=computer&ref=nb_sb_noss_2" # This should
be #URL 2
]
def parse(self, response):
items = AmazonItem()
title = response.xpath('//*[@class="a-size-base-plus a-color-base a-
text-normal"]/text()').extract()
asin = response.xpath('//*[@class ="a-link-normal"]/@href').extract()
# Note that I devided the products with ',,,' to make it easy to separate
# them. I am aware that this is not the best approach.
items['product_name'] = ',,,'.join(title).strip()
items['asin_product'] = ',,,'.join(asin).strip()
yield items
解决方案
首先,按类查询时推荐使用css。
现在到您的代码:
产品名称在 a 标签(产品 url)内。因此,您可以遍历链接并存储 URL 和标题。
<a class="a-link-normal a-text-normal" href="/adidas-Mens-Lite-Racer-Running/dp/B071P19D3X/ref=sr_1_3?keywords=shoes&qid=1554132536&s=gateway&sr=8-3">
<span class="a-size-base-plus a-color-base a-text-normal">Adidas masculina Lite Racer byd tênis de corrida</span>
</a>
您需要AmazonItem
在 csv 文件的每一行创建一个对象。
def parse(self, response):
# You need to improve this css selector because there are links which
# are not a product, this is why I am checking if title is None and continuing.
for product in response.css('a.a-link-normal.a-text-normal'):
# product is a selector
title = product.css('span.a-size-base-plus.a-color-base.a-text-normal::text').get()
if not title:
continue
# The selector is already the a tag, so we only need to extract it's href attribute value.
asin = product.xpath('./@href').get()
item = AmazonItem()
item['product_name'] = title.strip()
item['asin_product'] = asin.strip()
yield item
推荐阅读
- arrays - 创建数据并将其输入到嵌套数组中 - Ruby
- tsql - 在Tsql中查询多天的两个时间范围
- wordpress - 我的网站主页上没有链接可用
- python - 如何一一读取 RabbitMQ 队列消息
- java - 如何将 DTO 中的 ID 映射到实体上的类的 ID
- npm - 为什么我要检查 package-lock.json?
- docker - 在 CentOS7 VM 中运行超过四个 DB2 Container 会导致资源错误
- javascript - 需要帮助按日期排序数据
- rust - '恐慌!' 在使用 Json 对象并遍历字符串值时
- postgresql - 尽管删除了所有实例并重新安装了 postgresql 和 pgadmin 4,但仍无法连接服务器