python
  • scrapy
  • 2016-12-04 116 views 1 likes 
    1

    我從一些事件網站上獲取信息當我運行我的蜘蛛時我得到了這個錯誤,我想知道是否有人知道如何解決它。Scrapy信息不是JSON可序列化

    File "/usr/lib/python3.4/json/encoder.py", line 173, in default raise TypeError(repr(o) + " is not JSON serializable") 
        TypeError: <Selector xpath='.//a[@target="_top"]/text()' data='Artist Development Fellowship Informatio'> is not JSON serializable 
    

    我可以湊與文本:

    scrapy shell http://ofa.fas.harvard.edu/events 
    
    for event in response.xpath('.//article[@class="node node-event node-teaser article event-start clearfix"]'): 
    event.xpath('.//a[@target="_top"]/text()') 
    

    蜘蛛:

    import scrapy 
    
    
    class FAS(scrapy.Spider): 
        name ='fas' 
        start_urls = [ 
         'http://ofa.fas.harvard.edu/events', 
        ] 
    
        def parse(self, response): 
         for event in response.xpath('.//article[@class="node node-event node-teaser article event-start clearfix"]'): 
          yield { 
          'title' : event.xpath('.//a[@target="_top"]/text()'), 
          } 
    

    回答

    2

    的錯誤說:anything.xpath('...')是一種選擇,而不是你缺少添加.extract_first()字符串方法。

    anything.xpath('...').extract_first() 
    
    相關問題