python爬蟲獲取拉鉤網(wǎng)在線搜索招聘信息(超實(shí)用!)
# -*- encoding: utf-8 -*-
"""
"""
#? 導(dǎo)入相應(yīng)的包
import requests
import time
import json
from urllib.parse import quote
def get_page(url_start,url_parse,params,job):
? ?#? 創(chuàng)建一個session對象
? ?s = requests.Session()
? ?#? 用session對象發(fā)出get請求,請求首頁獲取cookies
? ?s.get(url_start, headers=headers, timeout=3)
? ?#? 獲取此處的cookie
? ?cookie = s.cookies
? ?#? 獲取此次的文本
? ?response = s.post(url_parse, data=params, headers=headers, cookies=cookie, timeout=3)
? ?#? 設(shè)置每次請求間隔5秒鐘
? ?time.sleep(5)
? ?#? 獲取json格式的數(shù)據(jù)
? ?json_data = json.loads(response.text)
? ?#? 從json數(shù)據(jù)中獲取到數(shù)據(jù)的總數(shù)
? ?total_Count = json_data['content']['positionResult']['totalCount']
? ?print("搜索結(jié)果一共有:"+str(total_Count)+"條")
? ?'''
? ? ? 拉勾網(wǎng)每頁是15條數(shù)據(jù),默認(rèn)只有30頁的信息,如果抓取信息的總數(shù)/15>30,就取30頁
? ?'''
? ?if int(total_Count/15) < 30 :
? ? ? ?page_number = int(total_Count/15)
? ?else:
? ? ? ?page_number = 30
? ?#? 根據(jù)頁數(shù)決定方法調(diào)用的次數(shù)
? ?for pn in range(1, page_number + 1):
? ? ? ?get_info(url_start, url_parse, pn, job)
? ? ? ?print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
def get_info(url_start,url_parse,page_number,job):
? ? print("正在爬取第"+str(page_number)+"頁數(shù)據(jù)")
? ? data = {
? ? ? ? 'first': 'false',
? ? ? ? 'pn': page_number, # 頁數(shù)
? ? ? ? 'kd': job? # 搜索的關(guān)鍵字
? ? }
? ? #? 創(chuàng)建一個session對象
? ? s = requests.Session()
? ? #? 用session對象發(fā)出get請求,請求首頁獲取cookies
? ? s.get(url_start, headers=headers, timeout=3)
? ? #? 獲取此處的cookie
? ? cookie = s.cookies
? ? #? 獲取此次的文本
? ? response = s.post(url_parse, data=data, headers=headers, cookies=cookie, timeout=3)
? ? #? 設(shè)置爬蟲休眠5秒
? ? time.sleep(5)
? ? #? 整理json格式
? ? text = json.loads(response.text)
? ? #? 獲取信息列表
? ? info = text['content']['positionResult']['result']
? ? for i in info :
? ? ? ? # 獲取公司id
? ? ? ? print('公司id', i['companyId'])
? ? ? ? # 獲取公司全名
? ? ? ? print('公司全名', i['companyFullName'])
? ? ? ? # 獲取位置
? ? ? ? print('位置', i['city'])
? ? ? ? # 獲取薪資
? ? ? ? print('薪資', i['salary'])
? ? ? ? # 獲取公司規(guī)模
? ? ? ? print('公司所在人數(shù)', i['companySize'])
? ? ? ? # 獲取要求技能
? ? ? ? print('所需技能', i['skillLables'])
? ? ? ? # 招聘信息發(fā)布時間
? ? ? ? print('信息發(fā)布時間', i['createTime'])
? ? ? ? # 區(qū)域
? ? ? ? print('所在區(qū)域', i['district'])
? ? ? ? # 要求學(xué)歷
? ? ? ? print('要求學(xué)歷', i['education'])
? ? ? ? #? 車站名稱
? ? ? ? print('車站名稱', i['stationname'])
? ? ? ? print("===========================================================")
if __name__ == '__main__':? ? ? #? 主程序入口
? ? job = input("歡迎來到拉勾網(wǎng),請輸入您想咨詢的職位:")
? ? # 想要在URL中拼湊中文,需要將中文內(nèi)容進(jìn)行【轉(zhuǎn)碼】,用16進(jìn)制表示
? ? job_transcode = quote(job)
? ? print("job", job_transcode)
? ? #? 主url【要爬取數(shù)據(jù)頁面首頁】
? ? url_start = "https://www.lagou.com/jobs/list_"+job_transcode+"/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput="
? ? #? ajax請求
? ? url_parse = "https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false"
? ? print(url_start)
? ? params = {
? ? ? ? 'first': 'true',
? ? ? ? 'pn': '1',
? ? ? ? 'kd': job
? ? }
? ? # 加入請求頭,偽裝成瀏覽器
? ? headers = {
? ? ? ? 'Accept': 'application/json, text/javascript, */*; q=0.01',
? ? ? ? 'Referer': 'https://www.lagou.com/jobs/list_'+job_transcode+'/p-city_undefined?&cl=false&fromSearch=true&labelWords=&suginput=',
? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
? ? }
? ? get_page(url_start, url_parse, params, job)
效果圖

? ? ? ? 細(xì)心的小伙伴可能發(fā)現(xiàn)了,這次的爬蟲程序跟上次分享的圖片下載器完全不是一個級別的,怎么那么多處代碼看不懂 ̄へ ̄其實(shí)很正常啦,因?yàn)榕老x的下限很低,上限極高。(如果每個網(wǎng)站的數(shù)據(jù)都那么好獲取,那讓該網(wǎng)站背后的程序員情何以堪)可能對于初學(xué)爬蟲不久的學(xué)者來說,理解該次爬蟲的過程比較的困難。但莫言先生曾經(jīng)說過:
當(dāng)你的才華還撐不起你的野心的時候,
你就應(yīng)該靜下心來學(xué)習(xí);
當(dāng)你的能力還駕馭不了你的目標(biāo)時,
就應(yīng)該沉下心來,歷練。