本篇代碼提供者: 青燈教育-巳月老師 知識點: 動態數據抓包 requests發送請求 json數據解析 開發環境: 運行代碼 python 3.8 輔助敲代碼 pycharm 2021.2 第三方模塊 requests 如果安裝python第三方模塊: win + R 輸入 cmd 點擊確定, 輸 ...
本篇代碼提供者: 青燈教育-巳月老師
知識點:
- 動態數據抓包
- requests發送請求
- json數據解析
開發環境:
運行代碼
- python 3.8
輔助敲代碼
- pycharm 2021.2
第三方模塊
- requests
如果安裝python第三方模塊:
- win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
- 在pycharm中點擊Terminal(終端) 輸入安裝命令
如何配置pycharm裡面的python解釋器?
-
選擇file(文件) >>> setting(設置) >>> Project(項目) >>> python interpreter(python解釋器)
-
點擊齒輪, 選擇add
-
添加python安裝路徑
pycharm如何安裝插件?
-
選擇file(文件) >>> setting(設置) >>> Plugins(插件)
-
點擊 Marketplace 輸入想要安裝的插件名字 比如:翻譯插件 輸入 translation / 漢化插件 輸入 Chinese
-
選擇相應的插件點擊 install(安裝) 即可
-
安裝成功之後 是會彈出 重啟pycharm的選項 點擊確定, 重啟即可生效
基本思路流程(通用)
- 發送請求
- 獲取數據
- 解析數據
- 保存數據
代碼
導入模塊
import requests # 發送請求 訪問網站
import re
加入偽裝
url = 'https://www.kuaishou.com/graphql' # 偽裝 headers = { 'content-type': 'application/json', 'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_ea128125517a46bd491ae9ccb255e242; client_key=65890b29; didv=1646739254078; _bl_uid=pCldq3L00L61qCzj6fytnk2wmhz5; userId=270932146; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABJBZGcI4Czt3EqaG90aWFm2EPPYcAQlkuV3ZOkHUBcqEtWV--udx6stXFOhEkGgx4tNCBS9Vhl-GstWLkvn-r_eV1072IPsO8d5sUcUuTJv3nicPWVBcfHW813ST2a4uN7HyHsnpnRkjx2BFXoMRmdSO4tbAgy3-3QRTaw05tiEp79qGRfQgVmK4kVJLOTF9X7o9vSjrLrTaRxEf0rwsXJhoSsmhEcimAl3NtJGybSc8y6sdlIiCkLEL3ZiZwp85TGjXIHaw7KGda_VNpfdZ1qigsOkLmESgFMAE; kuaishou.server.web_ph=ad8a744eb59aab3bf509625671ad16837e66', 'Host': 'www.kuaishou.com', 'Origin': 'https://www.kuaishou.com', 'Referer': 'https://www.kuaishou.com/search/video?searchKey=%E5%8F%8C%E9%A9%AC%E5%B0%BE', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36', } # 傳數據 for page in range(1, 10): json = { 'operationName': "visionSearchPhoto", 'query': "fragment photoContent on PhotoEntity {\n id\n duration\n caption\n likeCount\n viewCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n __typename\n}\n\nfragment feedContent on Feed {\n type\n author {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n }\n photo {\n ...photoContent\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n ...feedContent\n __typename\n }\n searchSessionId\n pcursor\n aladdinBanner {\n imgUrl\n link\n __typename\n }\n __typename\n }\n}\n", 'variables': {'keyword': "雙馬尾", 'pcursor': str(page), 'page': "search", 'searchSessionId': "MTRfMjcwOTMyMTQ2XzE2NTU3MjcyNTU3NjFf5Y-M6ams5bC-XzUwMDQ"} }
1. 發送請求
response = requests.post(url=url, headers=headers, json=json)
2. 獲取數據
<Response [200]>: 請求成功
feeds = response.json()['data']['visionSearchPhoto']['feeds'] for feed in feeds: author_id = feed['author']['id'] photo_id = feed['photo']['id'] print(author_id, photo_id) caption = feed['photo']['caption'] photoUrl = feed['photo']['photoUrl'] print(caption, photoUrl) caption = re.sub('[\\/:"?<>|\\n]', '', caption) json_like = { 'operationName': "visionVideoLike", 'query': "mutation visionVideoLike($photoId: String, $photoAuthorId: String, $cancel: Int, $expTag: String) {\n visionVideoLike(photoId: $photoId, photoAuthorId: $photoAuthorId, cancel: $cancel, expTag: $expTag) {\n result\n __typename\n }\n}\n", 'variables': { 'cancel': 0, 'expTag': "1_i/2005246154318902369_xpcwebsearchxxnull0", 'photoAuthorId': author_id, 'photoId': photo_id } } resp_ = requests.post(url=url, headers=headers, json=json_like) # video_data = requests.get(photoUrl).content # with open(f'video/{caption}.mp4', mode='wb') as f: # f.write(video_data)
尾語
好了,我的這篇文章寫到這裡就結束啦!
有更多建議或問題可以評論區或私信我哦!一起加油努力叭(ง •_•)ง
喜歡就關註一下博主,或點贊收藏評論一下我的文章叭!!!