前言 嗨嘍,大家好呀~這裡是愛看美女的茜茜吶 又到了學Python時刻~ 第三方模塊: requests >>> pip install requests 如果安裝python第三方模塊: win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip instal ...
前言
嗨嘍,大家好呀~這裡是愛看美女的茜茜吶
又到了學Python時刻~
第三方模塊:
- requests >>> pip install requests
如果安裝python第三方模塊:
-
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
-
在pycharm中點擊Terminal(終端) 輸入安裝命令
環境介紹:
-
python 3.8 解釋器
-
pycharm 編輯器
-
ffmpeg 音視頻合成軟體
代碼實現:
-
發送請求
-
獲取數據
-
解析數據
-
保存數據
代碼
import requests # 第三方模塊 工具 訪問網站的
import re # 正則表達式模塊 內置模塊 無需安裝
import json
import base64
# 偽裝 headers = { # 用戶信息 'cookie': 'MONITOR_WEB_ID=d60c8d6b-f9fd-4b42-87ed-d7f0dd177d84; s_v_web_id=verify_l6c4goey_Fh9d4TZc_AmaK_4BRa_BLeh_B5vXZ6H781yo; passport_csrf_token=2dbecb46b188ac34405333e002497f32; passport_csrf_token_default=2dbecb46b188ac34405333e002497f32; BD_REF=1; support_webp=true; support_avif=true; sid_guard=5a594d8ffa24987c9089e04fae66e393%7C1662550605%7C3024000%7CWed%2C+12-Oct-2022+11%3A36%3A45+GMT; uid_tt=5384c3b25787b6d639046fa229fab410; uid_tt_ss=5384c3b25787b6d639046fa229fab410; sid_tt=5a594d8ffa24987c9089e04fae66e393; sessionid=5a594d8ffa24987c9089e04fae66e393; sessionid_ss=5a594d8ffa24987c9089e04fae66e393; sid_ucp_v1=1.0.0-KDgwOGQ4MGI4OWY4YzdiMGRhODVkOWNhY2RhMjlhMWFkNzM4MWJlMjUKFAjo5IrYFxDNhOKYBhgYIAw4CEAFGgJsZiIgNWE1OTRkOGZmYTI0OTg3YzkwODllMDRmYWU2NmUzOTM; ssid_ucp_v1=1.0.0-KDgwOGQ4MGI4OWY4YzdiMGRhODVkOWNhY2RhMjlhMWFkNzM4MWJlMjUKFAjo5IrYFxDNhOKYBhgYIAw4CEAFGgJsZiIgNWE1OTRkOGZmYTI0OTg3YzkwODllMDRmYWU2NmUzOTM; __ac_signature=_02B4Z6wo00f01HEALmwAAIDBEgruBOEFt9hxICrAAH9gv9rvSZZuqiB0o0tboJ0vQ97xgpdUySya065pz20yeFgC1Yrz35jV.b8cHjdbRm-i7w8W-jifqtBdIA0LFeUHtfCY4.NYmLy13hutae; _tea_utm_cache_1300=undefined; odin_tt=704235ed5afa6910dc0b5ffb994bb5d3daf9c0591b868ff9da14f5393de8bc2fffc97916402e1ff35ecb9fb0af2a7e08; tt_scid=CRHRc1Kwkz.aeOXFeILnmZSqNYZcf7DM.TjaByz0zHNUap-3q9c0l9byzlYzTrHG0a81; ttwid=1%7CaXGLP4hVSb3AgEBeaA89sdnboUoIVkitcI14pne1BV4%7C1662550872%7Cafe39b6a79fbdc545c4dfcd0676cf3207ed4522534434f8c08d06cc62d22f819; ixigua-a-s=3; __ac_nonce=063188a2b001d4fe7a4be; msToken=TM5cvaKIWDxYdeBOLwcZ_yYIufnXuW0ubPWwbYXt12vFjzpdwIaSzwF6Ai8vmmvh6OQteexulP9-f2LGb15nyB42UmduwGvQBWOUgC8Kw3a1PZ-AeYVjSL12CEGhGzg=',
list_url = 'https://www..com/api/videov2/author/new_video_list?to_user_id=105191269248&offset=30&limit=30&maxBehotTime=1597400514&order=new&isHome=0&msToken=aM8EWMWtHK4rLnnKWyb4MiUMbCo4P3e0FWO-HlKbQJL00ZxEmyzUWRVqcBhGbB3XxZmXMOjxlyApUDp8Z61SeuhL4ulGttJj86D7sI3Ssm-UW8jWre4_hj_R6EJOu74=&X-Bogus=DFSzswVLuvtANydASQJoVKXAIQ58&_signature=_02B4Z6wo000016eh55gAAIDCxKsn8AABWN-noeMAAIr5sacqPdq.nIwq052pRqOBr75PmVJ7xfDp9CRkNzQg3K5X7u-juF8axvWLBstGC4JwHVTiJ2yuZnXxL.H3DlOGWPDJFGcPX.7K2BRZ5d'
這裡嘚標誌性網址刪了,可以自己加一下哦~
繼續我們嘚代碼~嘿嘿
# 1. 發送請求 response = requests.get(url, headers=headers) # <Response [200]>: 請求成功了 # 伺服器 識別了你是一個爬蟲 # 如果發現你請求之後 中文無法正常的顯示 轉碼一下 response.encoding = 'utf-8' # 2. 獲取數據 html_data = response.text # 3. 解析數據 # 把 _SSR_HYDRATED_DATA=(.*?)</script> 後面的數據全部取出來 # 按照規律匹配字元串 # 搜索功能高級版 json_str = re.findall('_SSR_HYDRATED_DATA=(.*?)</script>', html_data)[0] json_str = json_str.replace('undefined', 'null') # 轉成 Python裡面 字典類型 # 字元串 json_dict = json.loads(json_str) title = json_dict['anyVideo']['gidInformation']['packerData']['video']['title'] main_url = json_dict['anyVideo']['gidInformation']['packerData']['video']['videoResource']['normal']['video_list']['video_4']['main_url'] video_url = base64.b64decode(main_url).decode() print(title) print(video_url) # 4. 保存數據 video_data = requests.get(video_url).content with open(f'mp4//{title}.mp4', mode='wb') as f: f.write(video_data)
效果
文章看不懂,我專門錄了對應的視頻講解,本文只是大致展示,完整代碼和視頻教程點擊下方藍字
==點擊 藍色字體 自取,我都放在這裡了。==