2016-12-17 50 views
-2

我想使用scrapy在網站https://www.germanystartupjobs.com上發佈所有工作。由於通過POST請求加載的作業,我把start_urls= ['https://www.germanystartupjobs.com/jm-ajax/get_listings/']。我在使用Chrome dev tool的命令method:POSTnetwork選項卡的第1頁中找到此URL如何查找網站上列出的所有工作?

我認爲在第二頁,我會得到不同的URL但是,在這裏似乎不是這種情況。我也試過用

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

生成更多的頁面與索引沒有幫助。我的代碼的當前版本在這裏:

import scrapy 
import json 
import re 
import textwrap 


class GermanyStartupJobs(scrapy.Spider): 

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

    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() 

     print "LENGTH = ", len(hrefs) 

     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 = '' 

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

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

我想從網站的至少17頁獲得類似信息。我怎麼能做到這一點,並改善我的代碼?獲得所需信息後,我計劃使用multi-threading加快此過程,並且nltk搜索海報名稱(如果可用)。

+1

我曾與整個部門的人誰的工作是寫好蜘蛛/ scrapers。我不完全確定這是有限的範圍,是一個很好的SO問題。 –

+0

感謝您的回答。正如你所看到的,我可以從第一頁獲得信息,並需要找出獲得其餘頁面的方法。如果您有經驗,相同的示例代碼或解釋將非常有幫助。我需要在今天完成這項工作 – Chak

回答

-1

您必須真正弄清楚數據在客戶端和服務器之間傳遞的方式,通過查看內容來以這種方式刮取網站。您想要的數據頁面如此精確,可能無法在URL中表示。

您是否分析過網站在網址中訪問網站時的網絡連接?它可能會從URL中提取內容,您也可以通過計算機可讀的方式訪問數據。這比挖掘網站要容易得多。

+0

我使用過的'URL'https://www.germanystartupjobs.com/jm-ajax/get_listings/'來自開發工具的網絡部分,從第一頁開始。現在,在所有其他頁面中,「url」仍然是相同的,這讓我想知道如何在進度中取得進展。你有我可以開始學習的教程嗎?我Google並嘗試自己,但是,如果你有一些知識,你也可以分享。 – Chak

相關問題