python使用requests庫(kù)爬取拉勾網(wǎng)招聘信息的實(shí)現(xiàn)
按F12打開(kāi)開(kāi)發(fā)者工具抓包,可以定位到招聘信息的接口

在請(qǐng)求中可以獲取到接口的url和formdata,表單中pn為請(qǐng)求的頁(yè)數(shù),kd為關(guān)請(qǐng)求職位的關(guān)鍵字


使用python構(gòu)建post請(qǐng)求
data = {
'first': 'true',
'pn': '1',
'kd': 'python'
}
headers = {
'referer': 'https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
res = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", data=data,headers=headers)
print(res.text)
發(fā)現(xiàn)沒(méi)有從接口獲取到數(shù)據(jù)

換了個(gè)網(wǎng)絡(luò)后接口還是會(huì)返回操作頻繁的錯(cuò)誤信息,仔細(xì)檢查后發(fā)現(xiàn)這個(gè)接口需要一個(gè)動(dòng)態(tài)的cookies不然會(huì)一值返回錯(cuò)誤頻繁
data = {
'first': 'true',
'pn': '1',
'kd': 'python'
}
#頭部中必須有user-agent和referer不然不會(huì)返回cookies
headers = {
'referer': 'https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
#通過(guò)訪問(wèn)主頁(yè)獲取cookies
r1= requests.get("https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='",headers=headers)
#再post請(qǐng)求中傳入cookies
r2 = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", data=data,headers=headers, cookies=r2.cookies)
print(r2.text)
注意!每請(qǐng)求十次接口cookies也會(huì)刷新一次,下面貼上完整爬蟲(chóng)代碼
import json
import logging
import requests
#獲取cookie
def getCookie():
res = requests.get("https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=",
headers=headers)
return res.cookies
#獲取json數(shù)據(jù)
def getPage(i, cookies, kw):
data = {
'first': 'true',
'pn': i,
'kd': kw
}
res = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", data=data,
headers=headers, cookies=cookies)
return json.loads(res.text)
#合并列表
def reduceList(l):
text = ""
for i in l:
text += i + " "
return text.strip()
#提取字段并保存到文件中
def saveInCsv(f, data):
js = data["content"]["positionResult"]["result"]
for node in js:
# 對(duì)空值進(jìn)行處理
district = node["district"]
if district != None:
district = "-" + district
else:
district = ""
f.write(
node["positionName"] + "·" + node["city"] + district + "·" + node[
"salary"] + "·" +
node["workYear"] + "·" + node["education"] + "·" + reduceList(node["skillLables"]) + "·" +
node["companyShortName"] + "·" + node["companySize"] + "·" + node["positionAdvantage"] + "\n")
if __name__ == '__main__':
#定義頭部
headers = {
'referer': 'https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
#初始化cookie
cookies = getCookie()
with open("file.csv", "w", encoding="utf-8") as f:
for i in range(1, 31):
#每十個(gè)請(qǐng)求重新獲取cookie
if (i % 10 == 0):
cookies = getCookie()
#解析字段并存儲(chǔ)
data = getPage(i, cookies, "python")
saveInCsv(f, data)
到此這篇關(guān)于python使用requests庫(kù)爬取拉勾網(wǎng)招聘信息的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python requests爬取拉勾網(wǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中requests庫(kù)的學(xué)習(xí)方法詳解
- Python爬蟲(chóng)之requests庫(kù)基本介紹
- python爬蟲(chóng)之requests庫(kù)的使用詳解
- Python HTTP庫(kù) requests 的簡(jiǎn)單使用詳情
- Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)
- python requests庫(kù)的使用
- python爬蟲(chóng)利器之requests庫(kù)的用法(超全面的爬取網(wǎng)頁(yè)案例)
- Python爬蟲(chóng)requests庫(kù)多種用法實(shí)例
- Python中requests庫(kù)的用法詳解
相關(guān)文章
Django上使用數(shù)據(jù)可視化利器Bokeh解析
這篇文章主要介紹了Django上使用數(shù)據(jù)可視化利器Bokeh解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python如何通過(guò)subprocess調(diào)用adb命令詳解
python可以說(shuō)是寫(xiě)一些小腳本的利器語(yǔ)法簡(jiǎn)單,做為最著名的就“膠水語(yǔ)言”用它來(lái)寫(xiě)一些命令腳本非常的方便。下面這篇文章主要給大家介紹了關(guān)于Python如何通過(guò)subprocess調(diào)用adb命令的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
解決python寫(xiě)入帶有中文的字符到文件錯(cuò)誤的問(wèn)題
今天小編就為大家分享一篇解決python寫(xiě)入帶有中文的字符到文件錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python ddt數(shù)據(jù)驅(qū)動(dòng)最簡(jiǎn)實(shí)例代碼
在本篇內(nèi)容里我們給大家分享了關(guān)于python ddt數(shù)據(jù)驅(qū)動(dòng)最簡(jiǎn)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們跟著學(xué)習(xí)下。2019-02-02
Pyecharts 中Geo函數(shù)常用參數(shù)的用法說(shuō)明
這篇文章主要介紹了Pyecharts 中Geo函數(shù)常用參數(shù)的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
基于Python實(shí)現(xiàn)中秋佳節(jié)月餅搶購(gòu)腳本
這篇文章主要介紹了Python版中秋佳節(jié)月餅搶購(gòu)腳本,今天要用的是一個(gè)測(cè)試工具的庫(kù)Selenium,今天我們就是用它去實(shí)現(xiàn)自動(dòng)化搶購(gòu)月餅,其實(shí)就是用這個(gè)工具"模擬"人為操作瀏覽器相應(yīng)的操作,比如登陸,勾選購(gòu)物車商品,下單購(gòu)買等等操作,需要的朋友可以參考下2022-09-09

