如何在不使用网络爬虫中的终端的情况下写入CSV文件



我需要一些帮助。我的抓取器现在正在运行,它创建了项目.csv文件,但只将product_name等放入其中......我该如何解决这个问题? 这是我的代码:

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:UsersOwnerDesktopscraperProjectScraperProjectitems.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()

在蜘蛛集的 settings.py 或custom_settings属性中

FEED_URI="location/csvfilename.csv"

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

相关内容

最新更新