2016-12-17 1 views
0

私はウェブサイトからいくつかのデータを掻き集め、item辞書の中に1つずつ格納しています。 geramny_startup_jobs.jsonファイル内にJSON形式のデータをすべて保存するにはどうすればよいですか?私のコードはここにある:私はscrapyウェブサイトからのモデリングのために、次のように述べたと、ファイル内のインポートされた別のファイルを作成しスクラップしたデータをJSONファイルの中で最も簡単に保存するにはどうすればよいですか?

import scrapy 
import json 
import re 
import textwrap 
import JobsItem from JobsItem 


class GermanyStartupJobs(scrapy.Spider): 

    name = 'JobsItem' 
    # start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/' + str(i) for i in range(1, 5)] 
    start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/'] 

    def parse(self, response): 

     data = json.loads(response.body) 
     html = data['html'] 
     selector = scrapy.Selector(text=data['html'], type="html") 
     hrefs = selector.xpath('//a/@href').extract() 

     for href in hrefs: 
      yield scrapy.Request(href, callback=self.parse_detail) 


    def parse_detail(self, response): 

     try: 
      full_d = str(response.xpath\ 
       ('//div[@class="col-sm-5 justify-text"]//*/text()').extract()) 

      full_des_li = full_d.split(',') 
      full_des_lis = [] 

      for f in full_des_li: 
       ff = "".join((f.strip().replace('\n', '')).split()) 
       if len(ff) < 3: 
        continue 
       full_des_lis.append(f) 

      full = 'u'+ str(full_des_lis) 

      length = len(full) 
      full_des_list = textwrap.wrap(full, length/3)[:-1] 

      full_des_list.reverse() 


      # get the job title    
      try: 
       title = response.css('.job-title').xpath('./text()').extract_first().strip() 
      except: 
       print "No title" 
       title = '' 

      # get the company name 
      try: 
       company_name = response.css('.company-title').xpath('./normal/text()').extract_first().strip() 
      except: 
       print "No company name" 
       company_name = '' 


      # get the company location 
      try: 
       company_location = response.xpath('//a[@class="google_map_link"]/text()').extract_first().strip() 
      except: 
       print 'No company location' 
       company_location = '' 

      # get the job poster email (if available)    
      try: 
       pattern = re.compile(r"(\w(?:[-.+]?\w+)+\@(?:[a-z0-9](?:[-+]?\w+)*\.)+[a-z]{2,})", re.I) 

       for text in full_des_list: 
        email = pattern.findall(text)[-1] 
        if email is not None: 
         break 
      except: 
       print 'No email' 
       email = '' 

      # get the job poster phone number(if available)       
      try: 
       r = re.compile(".*?(\(?\d{3}\D{0,3}\d{3}\D{0,3}\d{4}).*?", re.S) 
       phone = r.findall(full_des_list[0])[-1] 

       if phone is not None: 
        phone = '+49-' +phone 

      except: 
       print 'no phone' 
       phone = '' 


      # get the name of the poster (if available) 
      try: 
       for text in full_des_list: 
        names = get_human_names(text) 
        if len(names) != 0: 
         name = names[-1] 
         print name 
         break 
      except: 
       print 'no name found' 
       name = '' 

      item = { 
       'title': title, 
       'company name': company_name, 
       'company_location': company_location, 
       # 'poster name': name, 
       'email': email, 
       'phone': phone, 
       'source': u"Germany Startup Job" 
      } 
      yield item 


     except: 
      print 'Not valid' 
      # raise Exception("Think better!!") 

import scrapy 

class JobsItem(scrapy.Item): 
    title = scrapy.Field() 
    company_name = scrapy.Field() 
    company_location = scrapy.Field() 
    email = scrapy.Field() 
    phone = scrapy.Field() 
    source = scrapy.Field() 

次に、私はscrapy crawl JobsItem -o geramny_startup_jobs.jsonコマンドを実行すると動作しないようです。私は出力Scrapy 1.2.2 - no active projectを取得し、それは私が行うつもりはないこのコマンドを実行するためのプロジェクトを作成する必要があるということです。

更新:コマンドscrapy runspider file_name.py -o item.jsonとそのリトゥンターは、出力がクリアされていない形式になっています。それでもクリーンな出力を得る必要があります。

答えて

1

あなたのスパイダーでJobsItemクラスを使用していません。このコードで

item = { 
    'title': title, 
    'company name': company_name, 
    'company_location': company_location, 
    # 'poster name': name, 
    'email': email, 
    'phone': phone, 
    'source': u"Germany Startup Job" 
} 

:このコードを置き換えます。このように

item = JobsItem() 
item['title'] = title 
item['company_name'] = company_name 
item['company_location'] = company_location 
item['email'] = email 
item['phone'] = phone 
item['source'] = u"Germany Startup Job" 

を、あなたのクモは、アイテムのクラスではなく、単純な辞書を返します。これにより、フラグ-o geramny_startup_jobs.jsonを使用すると、Scrapyはアイテムをディスクに書き込むことができます。

関連する問題