[英]Saving scrapy results into csv file
我编写的Web搜寻器存在一些问题。 我想保存获取的数据。 如果我从scrapy教程中正确地理解了,我只需要屈服它,然后通过使用scrapy crawl <crawler> -o file.csv -t csv
来启动scrapy crawl <crawler> -o file.csv -t csv
对吗? 由于某种原因,文件保持为空。 这是我的代码:
# -*- coding: utf-8 -*-
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class PaginebiancheSpider(CrawlSpider):
name = 'paginebianche'
allowed_domains = ['paginebianche.it']
start_urls = ['https://www.paginebianche.it/aziende-clienti/lombardia/milano/comuni.htm']
rules = (
Rule(LinkExtractor(allow=(), restrict_css = ('.seo-list-name','.seo-list-name-up')),
callback = "parse_item",
follow = True),)
def parse_item(self, response):
if(response.xpath("//h2[@class='rgs']//strong//text()") != [] and response.xpath("//span[@class='value'][@itemprop='telephone']//text()") != []):
yield ' '.join(response.xpath("//h2[@class='rgs']//strong//text()").extract()) + " " + response.xpath("//span[@class='value'][@itemprop='telephone']//text()").extract()[0].strip(),
我正在使用python 2.7
如果查看Spider的输出,将会看到一堆类似以下错误消息的记录:
2018-10-20 13:47:52 [scrapy.core.scraper] ERROR: Spider must return Request, BaseItem, dict or None, got 'tuple' in <GET https://www.paginebianche.it/lombardia/abbiategrasso/vivai-padovani.html>
这意味着您没有得到正确的结果-您需要dict或Item
,而不是要创建的单项元组。
像这样简单的事情应该起作用:
yield {
'name': response.xpath("normalize-space(//h2[@class='rgs'])").get(),
'phone': response.xpath("//span[@itemprop='telephone']/text()").get()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.