单个爬虫文件使用scrapy保存为csv格式

本文介绍如何在不使用pipeline、item和settings文件的情况下,通过Scrapy单个爬虫文件直接将数据保存为CSV格式。在命令行中运行`scrapy runspider stackoverflower_spider.py -o abc.csv`即可实现。对于有多个spider的情况,可以使用`scrapy list`检查错误,并用`scrapy crawl name -o result.csv`来获取Excel格式的数据,因为Scrapy内未直接提供存储为Excel的功能。
摘要由CSDN通过智能技术生成
import scrapy

class StackOverflowSpider(scrapy.Spider):
	name = "stackoverflow"
	start_urls=["http://stackoverflow.com/questions?sort=votes"]

	def parse(self, response):
		for href in response.css('.question-summary h3 a::attr(href)'):
			full_url = response.urljoin(href.extract())
			yield scrapy.Request(full_url,callback=self.parse_question)

	def parse_question(self, response):
		yield {
			'title':response.css('h1 a::text').extract()[0],
			'votes':response.css(".question .vote-count-post::text").extract()[0],
			'body':response.css(".question .post-text").extract()[0],
			'tags': response.css('.question .post-tag::text').extract(),
			'link': response.url,
		}

这个文件不需要配置pipeline,item,setting等文件,如果想保存为cvs格式,在shell终端运行

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值