Python 保存Scrapy';启动URL';并正确地存储在数据帧中
我用刮刮刮刮一些网站数据。但我无法采取措施正确获取数据 这是我的代码的输出(请参见下面的代码): 在命令行中:Python 保存Scrapy';启动URL';并正确地存储在数据帧中,python,pandas,dataframe,scrapy,Python,Pandas,Dataframe,Scrapy,我用刮刮刮刮一些网站数据。但我无法采取措施正确获取数据 这是我的代码的输出(请参见下面的代码): 在命令行中: scrapy crawl myspider -o items.csv 输出: asin_product product_name ProductA,,,ProductB,,,ProductC,,, BrandA,,,BrandB,,,BrandC,,, ProductA,,,Prod
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
for title, asin in zip(title, asin):
item = AmazonItem()
item['product_name'] = title
item['asin_product'] = asin
yield item
PS:你应该查看amazons robots.txt。他们可能不允许您刮取他们的站点并禁止您的IP()
for title, asin in zip(title, asin):
item = AmazonItem()
item['product_name'] = title
item['asin_product'] = asin
yield item
PS:你应该查看amazons robots.txt。他们可能不允许你刮掉他们的网站并禁止你的IP()首先,这是一个错误
现在转到您的代码:
产品名称位于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>
首先,这是一个问题
现在转到您的代码:
产品名称位于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>
谢谢你的帮助,拉斐尔!但是这个解决方案没有像上面的例子那样“正确”地存储数据,对吗?我更新了我的答案,增加了输出格式@RoverFlow的步骤谢谢你的帮助Raphael!但是这个解决方案没有像上面的例子那样“正确”地存储数据,对吗?我更新了我的答案,增加了输出格式@roverflowThank@Luiz!它正在发挥作用,但有两点需要注意:1)。有可能看到URL的地址吗?就像这个例子一样,一个额外的列包含URL的?2). 我想您在这一行忘记了一个'asin=product.xpath('./@href).get()#至少,我在href后面加了逗号。3). 执行时,值保存在CSV文件中,但不确定原因。。但每行之后,都有一行是空的。这是因为“如果不继续”吗?非常感谢@Roverflow 1)您可以使用response.url获取生成响应的url。因此,您可以执行item['url']=response.url。2) 是的,我忘了一个。3) 我应该与csv相关。继续,将csv作为文本文件打开,然后查看文件中逗号的显示方式,可能这与您的csv查看器(excel,open(libre)office)有关。谢谢@Luiz!它正在发挥作用,但有两点需要注意:1)。有可能看到URL的地址吗?就像这个例子一样,一个额外的列包含URL的?2). 我想您在这一行忘记了一个'asin=product.xpath('./@href).get()#至少,我在href后面加了逗号。3). 执行时,值保存在CSV文件中,但不确定原因。。但每行之后,都有一行是空的。这是因为“如果不继续”吗?非常感谢@Roverflow 1)您可以使用response.url获取生成响应的url。因此,您可以执行item['url']=response.url。2) 是的,我忘了一个。3) 我应该与csv相关。继续,将csv作为文本文件打开,然后查看文件中逗号的显示方式,可能这与您的csv查看器(excel,open(libre)office)有关。