1. 程式人生 > 程式設計 >python爬取招聘要求等資訊例項

python爬取招聘要求等資訊例項

在我們人生的路途中,找工作是每個人都會經歷的階段,小編曾經也是苦苦求職大軍中的一員。懷著對以後的規劃和想象,我們在找工作的時候,會看一些招聘資訊,然後從中挑選合適的崗位。不過招聘的崗位每個公司都有不少的需求,我們如何從中獲取資料,來進行鍼對崗位方面的查詢呢?

大致流程如下:

1.從程式碼中取出pid

2.根據pid拼接網址 => 得到 detail_url,使用requests.get,防止爬蟲掛掉,一旦發現爬取的detail重複,就重新啟動爬蟲

3.根據detail_url獲取網頁html資訊 => requests - > html,使用BeautifulSoup

若爬取太快,就等著解封

if html.status_code!=200 print('status_code if {}'.format(html.status_code))

4.根據html得到soup => soup

5.從soup中獲取特定元素內容 => 崗位資訊

6.儲存資料到MongoDB中

程式碼:

# @author: limingxuan 
# @contect: [email protected]
# @blog: https://www.jianshu.com/p/a5907362ba72
# @time: 2018-07-21
import requests
from bs4 import BeautifulSoup
import time
from pymongo import MongoClient
headers = {  
  'accept': "application/json,text/javascript,*/*; q=0.01",'accept-encoding': "gzip,deflate,br",'accept-language': "zh-CN,zh;q=0.9,en;q=0.8",'content-type': "application/x-www-form-urlencoded; charset=UTF-8",'cookie': "JSESSIONID=""; __c=1530137184; sid=sem_pz_bdpc_dasou_title; __g=sem_pz_bdpc_dasou_title; __l=r=https%3A%2F%2Fwww.zhipin.com%2Fgongsi%2F5189f3fadb73e42f1HN40t8~.html&l=%2Fwww.zhipin.com%2Fgongsir%2F5189f3fadb73e42f1HN40t8~.html%3Fka%3Dcompany-jobs&g=%2Fwww.zhipin.com%2F%3Fsid%3Dsem_pz_bdpc_dasou_title; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1531150234,1531231870,1531573701,1531741316; lastCity=101010100; toUrl=https%3A%2F%2Fwww.zhipin.com%2Fjob_detail%2F%3Fquery%3Dpython%26scity%3D101010100; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1531743361; __a=26651524.1530136298.1530136298.1530137184.286.2.285.199",'origin': "https://www.zhipin.com",'referer': "https://www.zhipin.com/job_detail/?query=python&scity=101010100",'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/67.0.3396.99 Safari/537.36"
  }
conn = MongoClient('127.0.0.1',27017)
db = conn.zhipin_jobs
def init():
  items = db.Python_jobs.find().sort('pid')
  for item in items:
    if 'detial' in item.keys(): #當爬蟲掛掉時,跳過已爬取的頁
      continue
    detail_url = 'https://www.zhipin.com/job_detail/{}.html'.format(item['pid']) #單引號和雙引號相同,str.format()新格式化方式
    #第一階段順利打印出崗位頁面的url
    print(detail_url)
    #返回的html是 Response 類的結果
    html = requests.get(detail_url,headers = headers)
    if html.status_code != 200:
      print('status_code is {}'.format(html.status_code))
      break
    #返回值soup表示一個文件的全部內容(html.praser是html解析器)
    soup = BeautifulSoup(html.text,'html.parser')
    job = soup.select('.job-sec .text')
    print(job)
    #???
    if len(job)<1:
    item['detail'] = job[0].text.strip() #職位描述
    location = soup.select(".job-sec .job-location .location-address") 
    item['location'] = location[0].text.strip() #工作地點
    item['updated_at'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #實時爬取時間
    #print(item['detail'])
    #print(item['location'])
    #print(item['updated_at'])
    res = save(item) #呼叫儲存資料結構
    print(res)
    time.sleep(40)#爬太快IP被封了24小時==
#儲存資料到MongoDB中
def save(item):
  return db.Python_jobs.update_one({'_id':item['_id']},{'$set':item}) #why item ???
   
# 儲存資料到MongoDB
     
if __name__ == '__main__':
  init()

最終結果就是在MongoBooster中看到新增了detail和location的資料內容

python爬取招聘要求等資訊例項

到此這篇關於python爬取招聘要求等資訊例項的文章就介紹到這了,更多相關python爬蟲獲取招聘要求的程式碼內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!