2016-12-17 51 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

相关问题