2016-07-24 61 views
1

作为新用户,我设法制作了一个蜘蛛,可以连接电子商务网站并提取 每个产品的标题和变体以及输出CSV文件和产品系列,但我希望 这是一个变体,行,请有人可以帮助我在我的项目中前进。scrapy CSV写作

我期待着来到这个问题,但不幸的是我找不到答案。

我蜘蛛:

import scrapy 
from w3lib.html import remove_tags 
from products_crawler.items import ProductItem 


class DemostoreSpider(scrapy.Spider): 
    name = "demostore" 
    allowed_domains = ["adns-grossiste.fr"] 
    start_urls = [ 
     'http://adns-grossiste.fr/17-produits-recommandes', 
] 
download_delay = 0.5 

def parse(self, response): 
    for category_url in response.css('#categories_block_left > div > ul > li ::attr(href)').extract(): 
     yield scrapy.Request(category_url, callback=self.parse_category, meta={'page_number': '1'}) 

def parse_category(self, response): 
    for product_url in response.css('#center_column > ul > li > div > div.right-block > h5 > a ::attr(href)').extract(): 
     yield scrapy.Request(product_url, callback=self.parse_product) 

def parse_product(self, response): 
    item = ProductItem() 
    item['url'] = response.url 
    item['title'] = response.css('#center_column > div > div.primary_block.clearfix > div.pb-center-column.col-xs-12.col-sm-7.col- md-7.col-lg-7 > h1 ::text').extract_first() 
    item['Déclinaisons'] = remove_tags(response.css('#d_c_1852 > tbody >tr.combi_1852.\31 852_155.\31 852_26.odd > td.tl.sorting_1 > a > span ::text').extract_first() or '') 
    yield item 

样本CSV愿望: image CSV

+0

[Scrapy管道以正确格式导出csv文件的可能的副本](https://stackoverflow.com/questions/29943075/scrapy-pipeline-to-export-csv-file-in-the-right-format ) –

回答

1

退房official docummentation here

总之有两种做法,在simpliest一个将只是使用抓取命令参数简称--output-o。例如:

scrapy crawl myspider -o myspider.csv 

Scrapy会自动将转化项目转换为csv文件。有关更详细的方法,请查看开始处张贴的文档页面。