話說年末了,有作業的要考慮明年跳槽,沒作業的要考慮明年找作業,那么都想好怎么搞了嗎?

不知道怎么了解?沒事,來,我們用python一鍵查看并分析!
一、準備前戲
1、使用的軟體
python 3.8
pycharm 2021專業版 激活碼
2、使用的內置模塊
pprint >>> # 格式化輸入模塊 csv >>> # 保存csv檔案 re >>> # re 正則運算式 time >>> # 時間模塊
3、要安裝的第三方模塊
requests >>> # 資料請求模塊
win + R 輸入cmd 輸入安裝命令 pip install 模塊名 ,如果出現爆紅, 可能是因為網路連接超時,切換國內鏡像源即可,

二、思路流程
爬蟲就是模擬瀏覽器,對于服務器發送請求,得到它回傳回應的資料,
資料來源分析
首先確定目標,分析資料內容,可以從什么地方獲取,
資料是通過哪個url地址發送什么請求方式,攜帶了那些請求頭,然后得到資料(通過開發者工具進行抓包分析)
我們分析資料,是分析服務器回傳資料,而不是元素面板,elements 是元素面板,前端代碼渲染之后的內容,

代碼實作步驟
- 發送請求,對于我們剛剛分析的到url(資料包)地址發送請求,post請求,請求引數, header請求頭;
- 獲取資料,獲取回應體的資料內容,服務器回傳的資料;
- 決議資料,提取我們想要的內容,根據回傳的資料,選擇對應最適合的決議方式去提取資料;
- 保存資料,保存本地資料庫/文本/表格資料;
- 多頁爬取;
# 我還給大家準備了這些資料,直接在群里就可以免費領取了, # 一群:872937351 (群滿了的話加二群) # 二群:924040232 # python學習路線匯總 # 精品Python學習書籍100本 # Python入門視頻合集 # Python實戰案例 # Python面試題 # Python相關軟體工具/pycharm永久激活
三、代碼部分
兄弟們,最最最喜歡的環節到了,上代碼,

import requests # 資料請求模塊 import pprint # 格式化輸入模塊 import csv # 保存csv檔案 import time # 時間模塊 # 打開檔案 等會進行保存 mode 是保存方式 a 追加寫入 f = open('拉勾.csv', mode='a', encoding='utf-8', newline='') csv_writer = csv.DictWriter(f, fieldnames=[ '標題', '公司名字', '城市', '薪資', '經驗', '學歷', '詳情頁', ]) csv_writer.writeheader() # 寫入表頭 for page in range(1, 11): # 1. 發送請求 字串格式化輸出 {}占位符 print(f'===================正在爬取第{page}頁的資料內容===================') time.sleep(2) # 延時2秒鐘 url = 'https://www.lagou.com/jobs/v2/positionAjax.json' # 確定請求的url地址 # headers 請求頭 爬蟲就是模擬瀏覽器 對于服務器發送請求, 得到他回傳回應資料 # headers 作用 偽裝python代碼的 把python代碼偽裝成瀏覽器 去發送請求 簡單反爬一種手段 # user-agent 用戶代理 瀏覽器的身份標識 headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' } # data 請求引數, post請求 需要傳遞一個from data表單資料 # pycharm使用的小技巧一: 快速批量替換 選中 ctrl + R 輸入正則運算式匹配規則 # pycharm使用的小技巧二: 翻譯插件 可以去安裝 data =https://www.cnblogs.com/hahaa/p/ { 'first': 'true', 'needAddtionalResult': 'false', 'city': '全國', 'px': 'new', 'pn': page, 'fromSearch': 'true', 'kd': 'python', } # 通過requests這個模塊里面post請求方法 對于url地址發送請求, 并且給傳遞一個data請求引數, headers 請求頭, 最后response變數接收 response = requests.post(url=url, data=https://www.cnblogs.com/hahaa/p/data, headers=headers) # <Response [200]> 放回的結果 response 物件 200 狀態碼 表示請求成功 # 2. 獲取資料 response.json() 獲取json字典資料 response.text 獲取文本資料(字串資料) response.content 二進制資料 # print(response.text) # pprint.pprint(response.json()) # 3. 決議資料 字典資料型別, 決議資料 提取內容 可以根據鍵值對取值 根據冒號左邊的內容, 提取冒號右邊的內容 # 根據冒號左邊的內容, 提取冒號右邊的內容 result = response.json()['content']['positionResult']['result'] # pprint.pprint(result) for index in result: # for回圈 遍歷 提取串列里面每一個元素 title = index['positionName'] # 標題 company_name = index['companyFullName'] # 公司名字 city = index['city'] # 城市 money = index['salary'] # 薪資 workYear = index['workYear'] # 經驗 edu = index['education'] # 學歷 href = https://www.cnblogs.com/hahaa/p/f'https://www.lagou.com/wn/jobs/{index["positionId"]}.html' # json.loads() 字串資料轉字典 dit = { '標題': title, '公司名字': company_name, '城市': city, '薪資': money, '經驗': workYear, '學歷': edu, '詳情頁': href, } csv_writer.writerow(dit) print(dit)
兄弟們覺得還行的話,記得三連哈~

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/397149.html
標籤:Python
