Как записать в файл CSV без использования терминала в Webscraper - PullRequest
0 голосов
/ 13 июня 2018

Мне нужна помощь.Мой скребок теперь работает, и он создает файл items.csv, но только помещает в него имя_продукта и т. Д. .... как это исправить?Вот мой код как есть:

import scrapy
import csv

class ProductSetSpider(scrapy.Spider):
name = "Product_spider"
start_urls = ['https://www.grainger.com/category/bacharach/ecatalog/N-1z125ev']
FEED_URI= r"C:\Users\Owner\Desktop\scraperProject\ScraperProject\items.csv"
# custom_settings = {'FEED_EXPORT_FIELDS': ["product_name" , ],
# }
def parse(self, response):
    self.log('I just visited' + response.url)
    yield {
        'product_name': response.css('h2.list-view__product-heading::text').extract()
        #'product_detail' : response.css('').extract_first()
        #'product_rating' : response.css('').extract_first()
        #'product_category' : response.css('').extract_first()
        #'product_company' : response.css('').extract_first()
        }

    Output_file = open('items.csv', 'w') #items.csv is name of output file
    fieldnames = ['product_name', 'product_details', 'product_rating', 'product_category', 'product_company'] #adding header to file
    writer = csv.DictWriter(Output_file, fieldnames=fieldnames)
    writer.writeheader()
    for url in start_urls:
        writer.writerow({product_name: response.url(fieldnames, css)}) #writing data into file.
        file_name.close()

1 Ответ

0 голосов
/ 13 июня 2018

в атрибуте settings.py или custom_settings вашего паука

FEED_URI="location/csvfilename.csv"

https://doc.scrapy.org/en/latest/topics/feed-exports.html

...