一、本文目標
取得Ajax 要求,解析JSON 中所需欄位
資料儲存到Excel 中
資料儲存到MySQL, 方便分析
#二、分析結果
1.引入庫
五個城市Python 職位平均薪資水準
#2.頁面結構
我們輸入查詢條件以Python 為例,其他條件預設不選,點選查詢,就能看到所有Python 的職位了,然後我們打開控制台,點擊網路標籤可以看到如下請求:
從回應結果來看,這個請求正是我們需要的內容。後面我們直接請求這個地址就好了。從圖中可以看出 result 下面是各個崗位資訊。
到這裡我們知道了從哪裡請求數據,從哪裡取得結果。但 result 清單中只有第一頁 15 則數據,其他頁面數據怎麼取得呢?
3.請求參數
我們點擊參數選項卡,如下:
發現提交了三個表單數據,很明顯看出來kd 是我們搜尋的關鍵字, pn 就是當前頁碼。 first 預設就行了,不用管它。剩下的事情就是建構請求,來下載 30 個頁面的資料了。
4.建構請求 解析資料
建構請求很簡單,我們還是用 requests 函式庫來搞定。首先我們建構出表單資料
data = {'first': 'true', 'pn': page, 'kd': lang_name}
之後用 requests 來請求url位址,解析得到的 JSON 資料就算大功告成了。由於拉勾對爬蟲限制比較嚴格,我們需要把瀏覽器中 headers 字段全部加上,而且把爬蟲間隔調大一點,我後面設置的為 10-20s,然後就能正常獲取數據了。
import requests def get_json(url, page, lang_name): headers = { 'Host': 'www.lagou.com', 'Connection': 'keep-alive', 'Content-Length': '23', 'Origin': 'https://www.lagou.com', 'X-Anit-Forge-Code': '0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'X-Requested-With': 'XMLHttpRequest', 'X-Anit-Forge-Token': 'None', 'Referer': 'https://www.lagou.com/jobs/list_python?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=&suginput=', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7' } data = {'first': 'false', 'pn': page, 'kd': lang_name} json = requests.post(url, data, headers=headers).json() list_con = json['content']['positionResult']['result'] info_list = [] for i in list_con: info = [] info.append(i.get('companyShortName', '无')) info.append(i.get('companyFullName', '无')) info.append(i.get('industryField', '无')) info.append(i.get('companySize', '无')) info.append(i.get('salary', '无')) info.append(i.get('city', '无')) info.append(i.get('education', '无')) info_list.append(info) return info_list
4.獲取所有數據
了解如何解析數據,剩下的就是連續請求所有頁面了,我們構造一個函數來請求所有 30 頁的數據。
def main(): lang_name = 'python' wb = Workbook() conn = get_conn() for i in ['北京', '上海', '广州', '深圳', '杭州']: page = 1 ws1 = wb.active ws1.title = lang_name url = 'https://www.lagou.com/jobs/positionAjax.json?city={}&needAddtionalResult=false'.format(i) while page < 31: info = get_json(url, page, lang_name) page += 1 import time a = random.randint(10, 20) time.sleep(a) for row in info: insert(conn, tuple(row)) ws1.append(row) conn.close() wb.save('{}职位信息.xlsx'.format(lang_name)) if __name__ == '__main__': main()
以上是怎麼用Python實現職位分析報告的詳細內容。更多資訊請關注PHP中文網其他相關文章!

使用NumPy創建多維數組可以通過以下步驟實現:1)使用numpy.array()函數創建數組,例如np.array([[1,2,3],[4,5,6]])創建2D數組;2)使用np.zeros(),np.ones(),np.random.random()等函數創建特定值填充的數組;3)理解數組的shape和size屬性,確保子數組長度一致,避免錯誤;4)使用np.reshape()函數改變數組形狀;5)注意內存使用,確保代碼清晰高效。

播放innumpyisamethodtoperformoperationsonArraySofDifferentsHapesbyAutapityallate AligningThem.itSimplifififiesCode,增強可讀性,和Boostsperformance.Shere'shore'showitworks:1)較小的ArraySaraySaraysAraySaraySaraySaraySarePaddedDedWiteWithOnestOmatchDimentions.2)

forpythondataTastorage,choselistsforflexibilityWithMixedDatatypes,array.ArrayFormeMory-effficityHomogeneousnumericalData,andnumpyArraysForAdvancedNumericalComputing.listsareversareversareversareversArversatilebutlessEbutlesseftlesseftlesseftlessforefforefforefforefforefforefforefforefforefforlargenumerdataSets; arrayoffray.array.array.array.array.array.ersersamiddreddregro

Pythonlistsarebetterthanarraysformanagingdiversedatatypes.1)Listscanholdelementsofdifferenttypes,2)theyaredynamic,allowingeasyadditionsandremovals,3)theyofferintuitiveoperationslikeslicing,but4)theyarelessmemory-efficientandslowerforlargedatasets.

toAccesselementsInapyThonArray,useIndIndexing:my_array [2] accessEsthethEthErlement,returning.3.pythonosezero opitedEndexing.1)usepositiveandnegativeIndexing:my_list [0] fortefirstElment,fortefirstelement,my_list,my_list [-1] fornelast.2] forselast.2)

文章討論了由於語法歧義而導致的Python中元組理解的不可能。建議使用tuple()與發電機表達式使用tuple()有效地創建元組。 (159個字符)

本文解釋了Python中的模塊和包裝,它們的差異和用法。模塊是單個文件,而軟件包是帶有__init__.py文件的目錄,在層次上組織相關模塊。

文章討論了Python中的Docstrings,其用法和收益。主要問題:Docstrings對於代碼文檔和可訪問性的重要性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具