Python爬蟲實戰(zhàn)演練之采集拉鉤網(wǎng)招聘信息數(shù)據(jù)
本文要點:
- 爬蟲的基本流程
- requests模塊的使用
- 保存csv
- 可視化分析展示
環(huán)境介紹
- python 3.8
- pycharm 2021專業(yè)版 激活碼
- Jupyter Notebook
pycharm 是編輯器 >> 用來寫代碼的 (更方便寫代碼, 寫代碼更加舒適)
python 是解釋器 >>> 運行解釋python代碼的
本次目標
爬蟲塊使用
內(nèi)置模塊:
- import pprint >>> 格式化輸入模塊
- import csv >>> 保存csv文件
- import re >>> re 正則表達式
- import time >>> 時間模塊
第三方模塊:
- import requests >>> 數(shù)據(jù)請求模塊 pip install requests
win + R 輸入cmd,回車輸入安裝命令pip install 模塊名。
如果出現(xiàn)爆紅,可能是因為,網(wǎng)絡連接超時,切換國內(nèi)鏡像源
代碼實現(xiàn)步驟: (爬蟲代碼基本步驟)
- 發(fā)送請求
- 獲取數(shù)據(jù)
- 解析數(shù)據(jù)
- 保存數(shù)據(jù)
開始代碼
導入模塊
import requests # 數(shù)據(jù)請求模塊 第三方模塊 pip install requests import pprint # 格式化輸出模塊 import csv # csv保存數(shù)據(jù) import time
發(fā)送請求
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false' # headers 請求頭 用來偽裝python代碼, 防止被識別出是爬蟲程序, 然后被反爬 # user-agent: 瀏覽器的基本標識 headers = { 'cookie': 'privacyPolicyPopup=false; user_trace_token=20211016201224-ba4d90f0-3db5-4647-a86e-411ee3d5bfef; __lg_stoken__=08639898fbdd53a7ebf88fa16e895b59a51e47738f45faef6a32b9a88d6537bf9459b2c6d956a636a99ff599c6a260f04514df42cb77f83065d55f48a2549e60381e8da811b8; JSESSIONID=ABAAAECAAEBABIIE72FFC38A79322951663B5C7AF10CD12; WEBTJ-ID=20211016201225-17c89047f4293-0d7a7cd583dc83-b7a1438-2073600-17c89047f43a90; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2217c8904800d57b-04f17ed5193984-b7a1438-2073600-17c8904800e765%22%2C%22%24device_id%22%3A%2217c8904800d57b-04f17ed5193984-b7a1438-2073600-17c8904800e765%22%7D; PRE_UTM=; PRE_HOST=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist%5Fpython%3FlabelWords%3D%26fromSearch%3Dtrue%26suginput%3D; LGSID=20211016201225-7b8aa578-74ab-4b09-885c-ebbe57a6029a; PRE_SITE=; LGUID=20211016201225-fda15dbb-7823-4a2d-9d80-258caf018f02; _ga=GA1.2.903785807.1634386346; _gat=1; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1634386346; _gid=GA1.2.701447082.1634386346; X_HTTP_TOKEN=ba154973a88f2f64153683436141effc1d544fa2ed; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1634386352; LGRID=20211016201232-8913a057-d37d-41c3-b094-a04cf36515a7; SEARCH_ID=ff32d1294b464305b4e0907f659ef2a7', 'referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36', } data = { 'first': 'false', 'pn': page, 'kd': 'python', 'sid': 'bf8ed05047294473875b2c8373df0357' } # response 自定義變量 可以自己定義 response = requests.post(url=url, data=data, headers=headers)
<Response [200]> 獲取服務器給我們響應數(shù)據(jù)
解析數(shù)據(jù)
json數(shù)據(jù)最好解析 非常好解析, 就根據(jù)字典鍵值對取值
result = response.json()['content']['positionResult']['result'] # 循環(huán)遍歷 從 result 列表里面 把元素一個一個提取出來 for index in result: # pprint.pprint(index) # href = index['positionId'] href = f'https://www.lagou.com/jobs/{index["positionId"]}.html' dit = { '標題': index['positionName'], '地區(qū)': index['city'], '公司名字': index['companyFullName'], '薪資': index['salary'], '學歷': index['education'], '經(jīng)驗': index['workYear'], '公司標簽': ','.join(index['companyLabelList']), '詳情頁': href, } # ''.join() 把列表轉(zhuǎn)成字符串 '免費班車', csv_writer.writerow(dit) print(dit)
加翻頁
for page in range(1, 31): print(f'------------------------正在爬取第{page}頁-------------------------') time.sleep(1)
保存數(shù)據(jù)
f = open('招聘數(shù)據(jù).csv', mode='a', encoding='utf-8', newline='') csv_writer = csv.DictWriter(f, fieldnames=[ '標題', '地區(qū)', '公司名字', '薪資', '學歷', '經(jīng)驗', '公司標簽', '詳情頁', ]) csv_writer.writeheader() # 寫入表頭
運行代碼,得到數(shù)據(jù)
【付費VIP完整版】只要看了就能學會的教程,80集Python基礎入門視頻教學
到此這篇關于Python爬蟲實戰(zhàn)演練之采集拉鉤網(wǎng)招聘信息數(shù)據(jù)的文章就介紹到這了,更多相關Python 采集拉鉤網(wǎng)招聘信息數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python PIL/cv2/base64相互轉(zhuǎn)換實例
今天小編就為大家分享一篇python PIL/cv2/base64相互轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python神經(jīng)網(wǎng)絡facenet人臉檢測及keras實現(xiàn)
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡facenet人臉檢測及keras實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05python庫pycryptodom加密技術探索(公鑰加密私鑰加密)
這篇文章主要為大家介紹了python庫pycryptodom加密技術探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01