午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

python使用requests庫爬取拉勾網招聘信息的實現

瀏覽:15日期:2022-07-04 16:57:42

按F12打開開發者工具抓包,可以定位到招聘信息的接口

python使用requests庫爬取拉勾網招聘信息的實現

在請求中可以獲取到接口的url和formdata,表單中pn為請求的頁數,kd為關請求職位的關鍵字

python使用requests庫爬取拉勾網招聘信息的實現

python使用requests庫爬取拉勾網招聘信息的實現

使用python構建post請求

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)

發現沒有從接口獲取到數據

python使用requests庫爬取拉勾網招聘信息的實現

換了個網絡后接口還是會返回操作頻繁的錯誤信息,仔細檢查后發現這個接口需要一個動態的cookies不然會一值返回錯誤頻繁

data = { ’first’: ’true’, ’pn’: ’1’, ’kd’: ’python’}#頭部中必須有user-agent和referer不然不會返回cookiesheaders = { ’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’}#通過訪問主頁獲取cookiesr1= requests.get('https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=’',headers=headers)#再post請求中傳入cookiesr2 = requests.post('https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false', data=data,headers=headers, cookies=r2.cookies)print(r2.text)

注意!每請求十次接口cookies也會刷新一次,下面貼上完整爬蟲代碼

import jsonimport loggingimport requests#獲取cookiedef 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數據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: # 對空值進行處理 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): #每十個請求重新獲取cookie if (i % 10 == 0):cookies = getCookie() #解析字段并存儲 data = getPage(i, cookies, 'python') saveInCsv(f, data)

到此這篇關于python使用requests庫爬取拉勾網招聘信息的實現的文章就介紹到這了,更多相關python requests爬取拉勾網內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美日韩国产网站 | 亚洲影视精品 | 九九九网站 | 精品午夜久久 | 在线一区观看 | 亚洲天天影视 | 金瓶狂野欧美性猛交xxxx | 香蕉久久久久久 | 成年人免费看视频 | 亚洲一区视频在线 | 毛片av免费看 | 亚洲免费视频观看 | 中文字幕第35页 | 欧美日韩操 | 中文字幕第6页 | 蜜臀久久99精品久久久久久 | 久久成人黄色 | 国产在线专区 | 99热青青草 | 超碰成人免费 | 日本精品一区二区三区视频 | 三级经典在线 | 欧美视频一二区 | 亚洲影视网| 在线中文视频 | 成人a在线| 久久久精品国产 | 日本免费一区二区三区 | 久久人人澡| av在线不卡观看 | 黄色日韩视频 | 精品视频99 | 免费激情网 | 特级一级黄色片 | 在线免费观看黄色av | 欧美亚洲在线视频 | 人人亚洲 | 6080av| 男女嘿咻嘿咻视频 | 色伊人久久| 国内精品久久久久久 |