用Python爬取拉鉤網招聘職位資訊
阿新 • • 發佈:2019-02-10
本文實現自動爬取拉鉤網招聘資訊,並將爬取結果儲存在本地文字中(也可以將資料存入資料庫)
使用到的Python模組包(Python3):
1.urllib.request
2.urllib.parse
3.json
簡單分析:
1.在向伺服器傳送請求,需要傳入post引數
2.搜尋的職位列表資訊存在一個josn檔案中,可使用json模組解析
3.翻頁
本文使用while True和break結合,根據json中result的值是否為空來判斷當前要是否是最後一頁,也可以根據json檔案中pageSize和totalCount兩個欄位的值得出總的頁面數。
完整程式碼:
import urllib.request
import urllib.parse
import json
def open_url(url,page_num,keywords):
try:
#設定post請求引數
page_data=urllib.parse.urlencode([
('pn',page_num),
('kd',keywords)
])
#設定headers
page_headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0',
'Connection':'keep-alive',
'Host':'www.lagou.com',
'Origin':'https://www.lagou.com',
'Cookie' :'JSESSIONID=ABAAABAABEEAAJA8F28C00A88DC4D771796BB5C6FFA2DDA; user_trace_token=20170715131136-d58c1f22f6434e9992fc0b35819a572b; LGUID=20170715131136-13c54b92-691c-11e7-893a-525400f775ce; index_location_city=%E5%8C%97%E4%BA%AC; _gat=1; TG-TRACK-CODE=index_search; _gid=GA1.2.496231841.1500095497; _ga=GA1.2.1592435732.1500095497; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1500095497; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1500104021; LGSID=20170715143221-5b993c04-6927-11e7-a985-5254005c3644; LGRID=20170715153341-ec8dbfd2-692f-11e7-a989-5254005c3644; SEARCH_ID=d27de6042bdf4d508cf9b39616a98a0d',
'Accept':'application/json, text/javascript, */*; q=0.01',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Referer':'https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98?labelWords=&fromSearch=true&suginput=',
'X-Anit-Forge-Token':'None',
'X-Requested-With':'XMLHttpRequest'
}
#開啟網頁
req=urllib.request.Request(url,headers=page_headers)
content=urllib.request.urlopen(req,data=page_data.encode('utf-8')).read().decode('utf-8')
return content
except Exception as e:
print(str(e))
#獲取招聘職位資訊
def get_position(url,page_num):
try:
page_content=open_url(url,page_num,keywords)
data=json.loads(page_content)
content=data.get('content')
result=[('positionId','職位ID'),('positionName','職位名稱'),('salary','薪資'),('createTime','釋出時間'),('workYear','工作經驗'),('education','學歷'),('positionLables','職位標籤'),('jobNature','職位型別'),('firstType','職位大類'),('secondType','職位細類'),('positionAdvantage','職位優勢'),('city','城市'),('district','行政區'),('businessZones','商圈'),('publisherId','釋出人ID'),('companyId','公司ID'),('companyFullName','公司名'),('companyShortName','公司簡稱'),('companyLabelList','公司標籤'),('companySize','公司規模'),('financeStage','融資階段'),('industryField','企業領域'),('industryLables','企業標籤')]
positionResult=content.get('positionResult').get('result')
if(len(positionResult)>0):
for position in positionResult:
with open("position.txt",'a') as fh:
fh.write("---------------------------\n")
for r in result:
with open("position.txt",'a') as fh:
fh.write(str(r[1])+":"+str(position.get(r[0]))+"\n")
return len(positionResult)
except Exception as e:
print(str(e))
#爬取拉勾網招聘職位資訊
if __name__=="__main__":
#爬取起始頁
url='https://www.lagou.com/jobs/positionAjax.json?city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false'
#設定查詢的關鍵詞
keywords="資料探勘"
page_num=1
while True:
print("正在爬取第"+str(page_num)+"頁......")
result_len=get_position(url,page_num)
if(result_len>0):
page_num+=1
else:
break
print("爬取完成")
爬取結果: