爬取網(wǎng)址:https://www.lagou.com/
爬取信息:工作崗位等信息
爬取方式:json數(shù)據(jù)
存儲方式:MongoDB
拉勾網(wǎng)采用了異步加載技術(shù)和提交表單,可通過逆向工程爬取招聘信息履植,并存儲在MongoDB數(shù)據(jù)庫中架专。
①拉勾網(wǎng)上關(guān)于Python的招聘信息如下:
②通過“查看網(wǎng)頁源代碼”资柔,可以看出網(wǎng)頁元素不在網(wǎng)頁源代碼中溉苛,說明網(wǎng)頁采用了Ajax技術(shù)渺绒。
③此時可打開Chrome的開發(fā)者工具仅炊,選擇Network選項卡斗幼,選中XHR項,就可以看到加載招聘信息的文件抚垄。在Headers中可以看到請求的網(wǎng)址蜕窿,在Response中可以看到返回的信息,信息為Json格式督勺。Json數(shù)據(jù)需要登錄后才能訪問渠羞,若未登錄,看不到相關(guān)信息智哀。
④json信息比較復(fù)雜次询,還可以使用Preview標(biāo)簽來觀察。
⑤點擊“下一頁”或者指定頁數(shù)瓷叫,網(wǎng)址并未改變屯吊,即網(wǎng)頁也使用了Ajax技術(shù)。
⑥打開Chrome的開發(fā)者工具摹菠,選擇Network選項卡盒卸,選中XHR項,可以看到翻頁后的請求URL也沒變化次氨。究其原因蔽介,是因為網(wǎng)頁使用了Post的請求方法,可在提交表單數(shù)據(jù)中觀察到pn字段發(fā)生變化。
⑦通過Preview標(biāo)簽虹蓄,還可以看到Json數(shù)據(jù)中的招聘信息總數(shù)1414犀呼,以及每頁顯示的招聘信息為15個。目前拉勾網(wǎng)默認(rèn)只有30頁信息薇组。
代碼為:
import requests
url = "https://www.lagou.com/jobs/positionAjax.json"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3294.6 Safari/537.36',
'Cookie':'XXXX',
'Connection':'keep-alive',
'Host':'www.lagou.com',
'Origin':'https://www.lagou.com',
'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}
params = {'first':'true',
'pn':'1',
'kd':'python'
}
r = requests.post(url,data=params,headers=headers)
print(r.status_code)
import json
json_data = json.loads(r.text)
print(json.dumps(json_data, indent=4, sort_keys=False, ensure_ascii=False)) ##結(jié)構(gòu)化json數(shù)據(jù)
結(jié)果為:
通過觀察json結(jié)構(gòu)外臂,可以很簡單地獲取相關(guān)信息:
import json
json_data = json.loads(r.text)
# ok = json_data['success'] ##測試是否獲取了正確的json數(shù)據(jù)。若正確律胀,返回True
# print(ok)
totalcount = json_data['content']['positionResult']['totalCount'] #獲取招聘總數(shù)量
totalpage = int(totalcount/15) #獲取總頁數(shù)
pagenum = totalpage if totalpage<30 else 30 #通過計算得到需要獲取的頁數(shù)宋光,用于改變pn值。
print(totalpage,pagenum)
results = json_data['content']['positionResult']['result']
for result in results:
companyId = result['companyId']
position_name = result['positionName']
workyear = result['workYear']
jobNature = result['jobNature']
financeStage = result['financeStage']
industryField = result['industryField']
city = result['city']
salary = result['salary']
positionId = result['positionId']
positionAdvantage = result['positionAdvantage']
companyShortName = result['companyShortName']
district = result['district']
createTime = result['createTime']
companyFullName= result['companyFullName']
print(companyFullName,companyShortName,companyId,position_name,salary)
結(jié)果為:
多頁面炭菌,存放進(jìn)MongoDB的代碼:
import requests
import json
import pymongo
import time
client = pymongo.MongoClient('localhost',27017)
mydb = client['mydb']
lagouzhaopin_info = mydb['lagouzhaopin_info']
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3294.6 Safari/537.36',
'Cookie':'xxxx',
'Connection':'keep-alive',
'Host':'www.lagou.com',
'Origin':'https://www.lagou.com',
'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}
def get_page(url):
params = {'first':'true','pn':'1','kd':'python'}
r = requests.post(url,data=params,headers=headers)
json_data = json.loads(r.text)
totalcount = json_data['content']['positionResult']['totalCount'] #獲取招聘總數(shù)量
totalpage = int(totalcount/15) #獲取總頁數(shù)
pagenum = totalpage if totalpage<30 else 30 #通過計算得到需要獲取的頁數(shù)罪佳,用于改變pn值。
return pagenum
def get_info(url,params):
r = requests.post(url,data=params,headers=headers)
json_data = json.loads(r.text)
results = json_data['content']['positionResult']['result']
for result in results:
companyFullName= result['companyFullName']
companyShortName = result['companyShortName']
city = result['city']
district = result['district']
position_name = result['positionName']
workyear = result['workYear']
jobNature = result['jobNature']
industryField = result['industryField']
salary = result['salary']
positionAdvantage = result['positionAdvantage']
info = {'公司全名':companyFullName,
'公司簡稱':companyShortName,
'城市':city,
'地區(qū)':district,
'職位':position_name,
'工作年限':workyear,
'職業(yè)性質(zhì)':jobNature,
'職業(yè)分類':industryField,
'工資':salary,
'公司優(yōu)勢':positionAdvantage
}
lagouzhaopin_info.insert_one(info)
#print(companyFullName,companyShortName,city,district,position_name,workyear,jobNature,industryField,salary,positionAdvantage)
if __name__ == '__main__':
url = "https://www.lagou.com/jobs/positionAjax.json"
pagenum = get_page(url)
for pn in range(1,pagenum+1):
params = {'first':'true','pn':str(pn),'kd':'python'}
get_info(url,params)
time.sleep(2) #記得設(shè)置睡眠時間娃兽,否則運行可能會報錯菇民。