對拉勾網招聘信息作一次數據分析(上)--40行代碼拿下全部數據

環境:javascript

ubuntu : 16.04html

python : 3.5.2java

scrapy : 1.3.3python

編輯器 : vimgit

分析拉勾網(http://www.lagou.com)web

能夠看到在左側有着各行各業的招聘信息,今天就把各行各業的招聘都給拿下來×——×數據庫

建立一個scrapy爬蟲項目:ubuntu

scrapy startproject lagou

使用基本(basic)模板建立一個蜘蛛(spider):vim

scrapy genspider lagou1 www.lagou.com

整個項目目錄結構以下:app

配置settings.py文件:

設置請求頭(能夠根據本身須要設置):

DEFAULT_REQUEST_HEADERS = {                                                                                                            
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',                                             
    'Accept-Encoding':'gzip, deflate, sdch, br',                                                                                       
     'Accept-Language':'zh-CN,zh;q=0.8',                                                                                                
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',           
 }

配置ITEM_PIPELINES優先級:

ITEM_PIPELINES = {                                                                                                                     
    'lagou.pipelines.LagouPipeline': 300,                                                                                              
 }

'''爬太快對人家網站不太好'''',因此設置我設置0.5一次:

DOWNLOAD_DELAY = 0.5

最後配置圖以下

編寫items.py文件(item文件定義抓取數據格式):

由於這裏只須要職位的序號,薪水,公司名稱,職位名稱,公司位置。因此items編寫以下

import scrapy                                                                                                                          
from scrapy import Field
 class LagouItem(scrapy.Item):                                                                                                          
     # define the fields for your item here like:                                                                                       
     # name = scrapy.Field()                                                                                                            
     index=Field()
     salary=Field()
     company=Field()
     positionname=Field()
     position=Field()

用firefox裏面的Firebug工具分析主頁(http://www.lagou.com)左側的的方向網址:編寫代碼獲取各個方向的網址

由於存在一些url是"javascript:;"

因此用下面的if進行判斷:

if 'http' in u:

獲取各方向網址:

def home_parse(self,response):
   sel=scrapy.selector.Selector(response)
  #/html/body/div[2]/div[1]/div[1]/div[1]/div[1]
   dd=sel.xpath("//div[@class='menu_main job_hopping']")
   allurl=dd.xpath("//a/@href").extract()
   for u in allurl:
   if 'http' in u:
   yield scrapy.Request(u,callback=self.parse,dont_filter=True)

對每個方向的頁面進行抓取:

例如:

針對這種頁面編寫代碼以下:

def parse(self,response):
   sel=selector.Selector(response)
   dd=sel.xpath('//li[@class="con_list_item default_list"]')
   for d in dd:
   position=LagouItem()
   position['index']=d.xpath('@data-index').extract()
   position['salary']=d.xpath('@data-salary').extract()
   position['company']=d.xpath('@data-company').extract()
   position['position']=d.xpath('div[@class="list_item_top"]/div/div/a/span/em/text()').extract()
   position['positionname']=d.xpath('@data-positionname').extract()
   yield position
   if 'http' in purl:
   yield scrapy.Request(purl,callback=self.parse,dont_filter=True)

將存儲數據到文件中(pipelines.py是存儲數據的一個模塊,你能夠設置將數據存儲到數據庫,也能夠存儲到文件中):

from lagou.items import LagouItem
from scrapy import log
class LagouPipeline(object):
    def process_item(self, item, spider):
     w=open('position','a')                                                                              
      w.write(str(dict(item))+"\n")                                                                       
      w.close()                                                                                           
      log.msg("position added to file!!!",level=log.DEBUG,spider=spider)                                  
      return item

 

至此爬蟲代碼完成

進入項目根目錄執行下面命令,查看爬蟲是否有無語法錯誤:
scrapy list

接着運行爬蟲:

scrapy crawl lagou1

結果:

這裏我抓了大概三萬條信息,明天再作分析×××——×××

歡迎大牛前來指教。。。

項目地址:https://git.oschina.net/nanxun/lagou.git

相關文章
相關標籤/搜索