2017-01-21 89 views
0

我是Scrapy的新手。我想从日本网站上获取一些数据,但是当我运行下面的蜘蛛时,它不会在导出的文件上显示任何数据。有人能帮助我吗。Scrapy使用Scrapy的日本网站,但没有输出文件中的数据

导出为csv格式并不会在shell中显示任何结果,只是[]

这是我的代码。

import scrapy 

class suumotest(scrapy.Spider): 

    name = "testsecond" 

    start_urls = [ 
     'https://suumo.jp/jj/chintai/ichiran/FR301FC005/?tc=0401303&tc=0401304&ar=010&bs=040' 
    ] 

    def parse(self, response): 
     # for following property link 
     for href in response.css('.property_inner-title+a::attr(href)').extract(): 
      yield scrapy.Request(response.urljoin(href), callback=self.parse_info) 



    # defining parser to extract data 
    def parse_info(self, response): 
     def extract_with_css(query): 
      return response.css(query).extract_first().strip() 

     yield { 
      'Title': extract_with_css('h1.section_title::text'), 
      'Fee': extract_with_css('td.detailinfo-col--01 span.detailvalue-item-accent::text'), 
      'Fee Descrition': extract_with_css('td.detailinfo-col--01 span.detailvalue-item-text::text'), 
      'Prop Description': extract_with_css('td.detailinfo-col--03::text'), 
      'Prop Address': extract_with_css('td.detailinfo-col--04::text'), 
     } 

回答

2

parse方法你的第一个CSS选择器有故障的位置:

response.css('.property_inner-title+a::attr(href)').extract() 

+是这里的故障。只需用空格代替它,如:

response.css('.property_inner-title a::attr(href)').extract() 

另一个问题是你定义extract_with_css()功能:

def parse_info(self, response): 
    def extract_with_css(query): 
     return response.css(query).extract_first().strip() 

这里的问题是,extract_first()会默认返回None如果发现.strip()没有值是string基类的函数,因为你没有得到一个字符串,这将引发一个错误。
为了解决这个问题,你可以默认值设置为extract_first是一个空字符串,而不是:

def parse_info(self, response): 
    def extract_with_css(query): 
     return response.css(query).extract_first('').strip() 
+0

没有解决的问题。现在得到一个错误'spider_exception/AttributeError:30'@Granitosaurus –

+0

@bassamfarooq你可以发布日志吗?好像现在你完全有一个不同的问题。你可以在linux上通过'scrapy crawl myspider&> output.log'命令产生一个日志。或者至少复制你得到的整个回溯错误信息。 – Granitosaurus

+0

@bassamfarooq我试过运行你的蜘蛛,发现错误,请参阅我的编辑。 – Granitosaurus