【发布时间】:2020-02-08 00:07:35
【问题描述】:
这是蜘蛛
import scrapy
import re
from ..items import HomedepotSpiderItem
class HomedepotcrawlSpider(scrapy.Spider):
name = 'homeDepotCrawl'
allowed_domains = ['homedepot.com']
start_urls = ['https://www.homedepot.com/b/ZLINE-Kitchen-and-Bath/N-5yc1vZhsy/Ntk-ProductInfoMatch/Ntt-zline?NCNI-5&storeSelection=3304,3313,3311,3310,8560&experienceName=default']
def parse(self, response):
items = HomedepotSpiderItem()
#get model
productName = response.css('.pod-plp__description.js-podclick-analytics').css('::text').getall()
productName = [x.strip(' ') for x in productName if len(x.strip())]
productName = [x.strip('\n') for x in productName if len(x.strip())]
productName = [x.strip('\t') for x in productName if len(x.strip())]
productName = [x.strip(',') for x in productName if len(x.strip())]
#productName = productName[0].split(',') tried to split the list into indiviudal elements
productSKU = response.css('.pod-plp__model::text').getall()
#get rid of all the stuff i dont need
productSKU = [x.strip(' ') for x in productSKU] #whiteSpace
productSKU = [x.strip('\n') for x in productSKU]
productSKU = [x.strip('\t') for x in productSKU]
productSKU = [x.strip(' Model# ') for x in productSKU] #gets rid of the model name
productSKU = [x.strip('\xa0') for x in productSKU] #gets rid of the model name
#get the price
productPrice = response.css('.price__numbers::text').getall()
#get rid of all the stuff i dont need
productPrice = [x.strip(' ') for x in productPrice if len(x.strip())]
productPrice = [x.strip('\n') for x in productPrice if len(x.strip())]
productPrice = [x.strip('\t') for x in productPrice if len(x.strip())]
productPrice = [x.strip('$') for x in productPrice if len(x.strip())]
## All prices are printing out twice, so take every other price
productPrice = productPrice[::2]
items['productName'] = productName
items['productSKU'] = productSKU
items['productPrice'] = productPrice
yield items
Items.py
import scrapy
class HomedepotSpiderItem(scrapy.Item):
#create items
productName = scrapy.Field()
productSKU = scrapy.Field()
productPrice = scrapy.Field()
#prodcutNumRating = scrapy.Field()
pass
我的问题
我现在正在用 Scrapy 做一些练习,我使用 CSS 从 Home Depot 的网站上提取了所有这些数据。提取后,我手动剥离了所有我不需要的数据,并且在终端上看起来很好。 但是,在将所有内容导出到 Excel 后,我提取的所有数据都打印到每行一列中。例如:产品名称->所有模型进入一个单元格。我查看了一些scrapy文档,发现 .getall() 将所有内容作为列表返回,所以我尝试将列表拆分为单个元素,认为这会很好,但是,这将摆脱我抓取的所有数据。
任何帮助都将不胜感激,如果需要任何澄清,请告诉我!
编辑 我正在使用以下方法导出到 excel:scrapy crawl homeDepotCrawl -o test.csv -t csv
【问题讨论】:
-
你能用你用来尝试将它导出到 excel 的代码更新你的问题吗?
-
我在下面给出了一个完整的答案,应该可以 100% 解决您的问题。我认为您只是误解了 scrapy.Item 的工作原理。它一次处理电子表格/json中的一项或一行。每个scrapy.Item 实例在返回或产生时都会输出一行。
-
@KrisztianToth 编辑了我的问题