2017-08-27 86 views
-1

我已经在python scrapy中编写了一个非常小的脚本来解析黄页网站中多个页面显示的姓名,街道和电话号码。当我运行我的脚本时,我发现它运行顺利。但是,我遇到的唯一问题是数据在csv输出中被抓取的方式。它总是两行之间的一行(行)间隙。我的意思是:数据正在每隔一行打印。看到下面的图片,你会明白我的意思。如果不是用于scrapy,我可以使用[newline ='']。但不幸的是,我在这里完全无奈。我如何摆脱csv输出中出现的空白行?预先感谢您看看它。无法摆脱csv输出中的空白行

items.py包括:

import scrapy 

class YellowpageItem(scrapy.Item): 
    name = scrapy.Field() 
    street = scrapy.Field() 
    phone = scrapy.Field() 

这里是蜘蛛:

import scrapy 

class YellowpageSpider(scrapy.Spider): 
    name = "YellowpageSp" 
    start_urls = ["https://www.yellowpages.com/search?search_terms=Pizza&geo_location_terms=Los%20Angeles%2C%20CA&page={0}".format(page) for page in range(2,6)] 

    def parse(self, response): 
     for titles in response.css('div.info'): 
      name = titles.css('a.business-name span[itemprop=name]::text').extract_first() 
      street = titles.css('span.street-address::text').extract_first() 
      phone = titles.css('div[itemprop=telephone]::text').extract_first() 
      yield {'name': name, 'street': street, 'phone':phone} 

这里是CSV输出看起来像:

enter image description here

顺便说一句,该命令我用来获取CSV输出是:

scrapy crawl YellowpageSp -o items.csv -t csv 
+0

我很快就说过了。这对我有效。我在投票答复和问题:D – 2017-12-02 18:08:39

回答

3

您可以通过创建一个新的FeedExporter来修复它。更改settings.py如下

FEED_EXPORTERS = { 
    'csv': 'project.exporters.FixLineCsvItemExporter', 
} 

在项目中创建一个exporters.py

exporters.py

import io 
import os 
import six 
import csv 

from scrapy.contrib.exporter import CsvItemExporter 
from scrapy.extensions.feedexport import IFeedStorage 
from w3lib.url import file_uri_to_path 
from zope.interface import implementer 


@implementer(IFeedStorage) 
class FixedFileFeedStorage(object): 

    def __init__(self, uri): 
     self.path = file_uri_to_path(uri) 

    def open(self, spider): 
     dirname = os.path.dirname(self.path) 
     if dirname and not os.path.exists(dirname): 
      os.makedirs(dirname) 
     return open(self.path, 'ab') 

    def store(self, file): 
     file.close() 


class FixLineCsvItemExporter(CsvItemExporter): 

    def __init__(self, file, include_headers_line=True, join_multivalued=',', **kwargs): 
     super(FixLineCsvItemExporter, self).__init__(file, include_headers_line, join_multivalued, **kwargs) 
     self._configure(kwargs, dont_fail=True) 
     self.stream.close() 
     storage = FixedFileFeedStorage(file.name) 
     file = storage.open(file.name) 
     self.stream = io.TextIOWrapper(
      file, 
      line_buffering=False, 
      write_through=True, 
      encoding=self.encoding, 
      newline="", 
     ) if six.PY3 else file 
     self.csv_writer = csv.writer(self.stream, **kwargs) 

我在Mac上,所以无法测试其Windows行为。但是,如果以上不起作用,然后改变部分代码,并设置newline="\n"

 self.stream = io.TextIOWrapper(
      file, 
      line_buffering=False, 
      write_through=True, 
      encoding=self.encoding, 
      newline="\n", 
     ) if six.PY3 else file 
+0

非常感谢塔伦。你真的是巨蟒。多年来我一直在遭受这个问题的困扰。创建并删除了几个线程来查找解决方案。你只是解决了一切。我希望我能够点击百万次按钮。顺便说一下,我可以在其他项目中使用它,因为除了跟上步伐之外,这对我来说非常巨大而且难以创造。 Thanksssssssssssssssss a zillion。 – SIM

+0

你可以在任何地方使用它 –

+0

最后一件事要知道我是不是已经太贪心了。有时候,列的位置改变了。虽然,这不是一个大问题,但我很想知道。你可以理解你是否看到链接。最重要的是,它与此线程无关,所以请随时忽略。 https://www.dropbox.com/s/tzztbli7v6quhc2/column%20order.txt?dl=0 – SIM