前言
嗨喽,大家好呀~这里是爱看美女的茜茜呐
又到了学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)
效果
文章看不懂,我专门录了对应的视频讲解,本文只是大致展示,完整代码和视频教程点击下方蓝字
==点击 蓝色字体 自取,我都放在这里了。==
尾语
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289808.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289808.html