python爬取招聘要求等信息實(shí)例
在我們?nèi)松穆吠局?,找工作是每個(gè)人都會(huì)經(jīng)歷的階段,小編曾經(jīng)也是苦苦求職大軍中的一員。懷著對(duì)以后的規(guī)劃和想象,我們?cè)谡夜ぷ鞯臅r(shí)候,會(huì)看一些招聘信息,然后從中挑選合適的崗位。不過(guò)招聘的崗位每個(gè)公司都有不少的需求,我們?nèi)绾螐闹蝎@取數(shù)據(jù),來(lái)進(jìn)行針對(duì)崗位方面的查找呢?
大致流程如下:
1.從代碼中取出pid
2.根據(jù)pid拼接網(wǎng)址 => 得到 detail_url,使用requests.get,防止爬蟲(chóng)掛掉,一旦發(fā)現(xiàn)爬取的detail重復(fù),就重新啟動(dòng)爬蟲(chóng)
3.根據(jù)detail_url獲取網(wǎng)頁(yè)html信息 => requests - > html,使用BeautifulSoup
若爬取太快,就等著解封
if html.status_code!=200 print('status_code if {}'.format(html.status_code))
4.根據(jù)html得到soup => soup
5.從soup中獲取特定元素內(nèi)容 => 崗位信息
6.保存數(shù)據(jù)到MongoDB中
代碼:
# @author: limingxuan # @contect: limx2011@hotmail.com # @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(): #當(dāng)爬蟲(chóng)掛掉時(shí),跳過(guò)已爬取的頁(yè) continue detail_url = 'https://www.zhipin.com/job_detail/{}.html'.format(item['pid']) #單引號(hào)和雙引號(hào)相同,str.format()新格式化方式 #第一階段順利打印出崗位頁(yè)面的url print(detail_url) #返回的html是 Response 類的結(jié)果 html = requests.get(detail_url,headers = headers) if html.status_code != 200: print('status_code is {}'.format(html.status_code)) break #返回值soup表示一個(gè)文檔的全部?jī)?nèi)容(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() #工作地點(diǎn) item['updated_at'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #實(shí)時(shí)爬取時(shí)間 #print(item['detail']) #print(item['location']) #print(item['updated_at']) res = save(item) #調(diào)用保存數(shù)據(jù)結(jié)構(gòu) print(res) time.sleep(40)#爬太快IP被封了24小時(shí)== #保存數(shù)據(jù)到MongoDB中 def save(item): return db.Python_jobs.update_one({'_id':item['_id']},{'$set':item}) #why item ??? # 保存數(shù)據(jù)到MongoDB if __name__ == '__main__': init()
最終結(jié)果就是在MongoBooster中看到新增了detail和location的數(shù)據(jù)內(nèi)容
到此這篇關(guān)于python爬取招聘要求等信息實(shí)例的文章就介紹到這了,更多相關(guān)python爬蟲(chóng)獲取招聘要求的代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python tkinter實(shí)現(xiàn)日期選擇器
這篇文章主要為大家詳細(xì)介紹了Python tkinter實(shí)現(xiàn)日期選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02Python實(shí)現(xiàn)新版正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別
這篇文章主要介紹了基于Python實(shí)現(xiàn)新版正方系統(tǒng)滑動(dòng)驗(yàn)證碼識(shí)別算法和方案,文中示例代碼對(duì)我們的學(xué)習(xí)和工作有一定的幫助,感興趣的可以了解一下2021-12-12tensorflow中tf.reduce_mean函數(shù)的使用
這篇文章主要介紹了tensorflow中tf.reduce_mean函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04pyCharm 實(shí)現(xiàn)關(guān)閉代碼檢查
這篇文章主要介紹了pyCharm 實(shí)現(xiàn)關(guān)閉代碼檢查,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06使用PyTorch常見(jiàn)4個(gè)錯(cuò)誤解決示例詳解
這篇文章主要為大家介紹了使用PyTorch常見(jiàn)4個(gè)錯(cuò)誤解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10win10安裝tensorflow-gpu1.8.0詳細(xì)完整步驟
這篇文章主要介紹了win10安裝tensorflow-gpu1.8.0詳細(xì)完整步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Python的NumPy使用之?dāng)?shù)組過(guò)濾
這篇文章主要介紹了Python的NumPy使用之?dāng)?shù)組過(guò)濾,在 NumPy中,我們使用布爾索引列表來(lái)過(guò)濾數(shù)組布爾索引列表是與數(shù)組中的索引相對(duì)應(yīng)的布爾值列表,需要的朋友可以參考下2023-07-07