2016-04-05 17 views
0

StackOverflow.com'u taramak ve atmak için Scrapy kullanıyorum. Bu so.pyScrapy'nin sonucu belirli bir JSON biçimine nasıl dışa aktarılır?

import scrapy 

class StackOverflowSpider(scrapy.Spider): 
    name = 'stackoverflow' 
    start_urls = ['http://stackoverflow.com'] 

    def parse(self, response): 
     for href in response.css('.question-summary h3 a::attr(href)'): 
      full_url = response.urljoin(href.extract()) 
      yield scrapy.Request(full_url, callback=self.parse_question) 

    def parse_question(self, response): 
     yield { 
      'link': response.url, 
     } 

Beklenen sonuç: so.json(geçerli JSON biçimi)

[ 
    "http://stackoverflow.com/questions/36421917/exponential-number-in-custom-number-format-of-excel", 
    "http://stackoverflow.com/questions/36421343/can-not-install-requirements-txt", 
    "http://stackoverflow.com/questions/36418815/difference-between-two-approaches-to-pass-parameters-to-web-server", 
    "http://stackoverflow.com/questions/36421743/sharing-an-oracle-database-connection-between-simultaneous-celery-tasks", 
    "http://stackoverflow.com/questions/36421941/jquery-add-css-style", 
] 

Sonra çalıştırın:

scrapy runspider so.py -o so.json 

sonucu değil beklendiği gibi. Buraya sıkıştım.

cevap

0

FEED_FORMAT=jsonlines ayarını kullanmayı deneyin. Eğer

[ 
    "https://stackoverflow.com/questions/36421917/exponential-number-in-custom-number-format-of-excel", 
    "https://stackoverflow.com/questions/36421343/can-not-install-requirements-txt", 
    "https://stackoverflow.com/questions/36418815/difference-between-two-approaches-to-pass-parameters-to-web-server", 
    "https://stackoverflow.com/questions/36421743/sharing-an-oracle-database-connection-between-simultaneous-celery-tasks", 
    "https://stackoverflow.com/questions/36421941/jquery-add-css-style", 
] 

Kendi ItemExporter yazmalısınız almak istiyorsanız

scrapy runspider so.py -o so.json --set FEED_FORMAT=jsonlines 

, see this question

+0

Bu sonuç, sonra yukarıdaki komutu çalıştırın: https://gist.github.com/donhuvy/7f75e0cf30ab0fe2ba79069ffa328b31 Hala beklenen sonuca benzemiyor. –

+0

Cevabı düzelttim, bir kez daha kontrol et. –

+0

Gözden geçirilmiş cevabınızı uyguladıktan sonra komut çalıştırıyorum, sonuçta https://gist.github.com/donhuvy/cc21a2a99b64fa367dbaec70f27b564c. Sonuç beklenmiyor. Sorunu çözmeme yardım et! –

İlgili konular