【发布时间】:2019-03-25 01:56:25
【问题描述】:
我编写的网络爬虫有一些问题。我想保存我获取的数据。如果我从scrapy教程中理解正确,我只需要放弃它,然后使用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
【问题讨论】:
标签: python python-2.7 scrapy web-crawler