標籤:asc 標籤 position localhost name requests 欄位 列表 als
前兩篇我們分別爬取了糗事百科和妹子圖網站,學習了 Requests, Beautiful Soup 的基本使用。不過前兩篇都是從靜態 HTML 頁面中來篩選出我們需要的資訊。這一篇我們來學習下如何來擷取 Ajax 請求返回的結果。
歡迎關注公號【智能製造專欄】學習更多原創智能製造及編程知識。
Python 爬蟲入門(二)——爬取妹子圖
Python 爬蟲入門(一)——爬取糗百
本篇以拉勾網為例來說明一下如何擷取 Ajax 請求內容
本文目標
- 擷取 Ajax 請求,解析 JSON 中所需欄位
- 資料儲存到 Excel 中
- 資料儲存到 MySQL, 方便分析
簡單分析
五個城市 Python 崗位平均薪資水平
Python 崗位要求學曆分布
Python 行業領域分布
Python 公司規模分布
查看頁面結構
我們輸入查詢條件以 Python 為例,其他條件預設不選,點擊查詢,就能看到所有 Python 的崗位了,然後我們開啟控制台,點擊網路標籤可以看到如下請求:
從響應結果來看,這個請求正是我們需要的內容。後面我們直接請求這個地址就好了。可以看出 result 下面就是各個崗位資訊。
到這裡我們知道了從哪裡請求資料,從哪裡擷取結果。但是 result 列表中只有第一頁 15 條資料,其他頁面資料怎麼擷取呢?
分析請求參數
我們點擊參數選項卡,如下:
發現提交了三個表單資料,很明顯看出來 kd 就是我們搜尋的關鍵詞,pn 就是當前頁碼。first 預設就行了,不用管它。剩下的事情就是構造請求,來下載 30 個頁面的資料了。
構造請求,並解析資料
構造請求很簡單,我們還是用 requests 庫來搞定。首先我們構造出表單資料 data = {‘first‘: ‘true‘, ‘pn‘: page, ‘kd‘: lang_name} 之後用 requests 來請求url地址,解析得到的 Json 資料就算大功告成了。由於拉勾對爬蟲限制比較嚴格,我們需要把瀏覽器中 headers 欄位全部加上,而且把爬蟲間隔調大一點,我後面設定的為 10-20s,然後就能正常擷取資料了。
import requestsdef 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
擷取所有資料
瞭解了如何解析資料,剩下的就是連續請求所有頁面了,我們構造一個函數來請求所有 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()
完整代碼
import randomimport timeimport requestsfrom openpyxl import Workbookimport pymysql.cursorsdef get_conn(): '''建立資料庫連接''' conn = pymysql.connect(host='localhost', user='root', password='root', db='python', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) return conndef insert(conn, info): '''資料寫入資料庫''' with conn.cursor() as cursor: sql = "INSERT INTO `python` (`shortname`, `fullname`, `industryfield`, `companySize`, `salary`, `city`, `education`) VALUES (%s, %s, %s, %s, %s, %s, %s)" cursor.execute(sql, info) conn.commit()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 # 返回列表def main(): lang_name = 'python' wb = Workbook() # 開啟 excel 活頁簿 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: # 每個城市30頁資訊 info = get_json(url, page, lang_name) page += 1 time.sleep(random.randint(10, 20)) for row in info: insert(conn, tuple(row)) # 插入資料庫,若不想存入 注釋此行 ws1.append(row) conn.close() # 關閉資料庫連接,不存資料庫 注釋此行 wb.save('{}職位資訊.xlsx'.format(lang_name))if __name__ == '__main__': main()
GitHub 地址:https://github.com/injetlee/Python/tree/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88
如果你想要爬蟲擷取的崗位資訊,請關注公號【智能製造專欄】後台留言發送 "python崗位"。
Python爬蟲——Python 崗位分析報告