VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • 西瓜视频视频颜值区如此惊艳,看我只用30行Python代码批量下载,保存本地慢慢

模块安装

本次需要使用的模块是requests 模块,没安装的小伙伴直接pip安装即可。

环境

本次使用的环境是Python3.8 ,编辑器是 pycharm 

效果展示


代码展示

使用的模块

import requests
import re
import json
import base64

 

发送请求

复制代码
headers = {
    'cookie': 'support_webp=true; support_avif=true; csrf_session_id=76ceeb6d60b0fcd804de9be6e9693c54; s_v_web_id=verify_lefmeh66_SLWSXhCD_aZkd_4NEx_BgaI_hGUNbdQrHIpi; MONITOR_WEB_ID=56b4a269-39b6-4147-ab7c-9195b568c5e8; _tea_utm_cache_2018=undefined; passport_csrf_token=1a7dd3b7b20888e47197ea1d942e17d5; passport_csrf_token_default=1a7dd3b7b20888e47197ea1d942e17d5; passport_auth_status=121a29188cf1b9ecf308efbe3d0920c5%2C; passport_auth_status_ss=121a29188cf1b9ecf308efbe3d0920c5%2C; sid_guard=d75c6025dd5f4e3d658be37aad5f91b5%7C1677067151%7C3024000%7CWed%2C+29-Mar-2023+11%3A59%3A11+GMT; uid_tt=2f228913ef6379e66da0c3399feaa580; uid_tt_ss=2f228913ef6379e66da0c3399feaa580; sid_tt=d75c6025dd5f4e3d658be37aad5f91b5; sessionid=d75c6025dd5f4e3d658be37aad5f91b5; sessionid_ss=d75c6025dd5f4e3d658be37aad5f91b5; sid_ucp_v1=1.0.0-KGQ4Nzc4MjZiYWI1NWRmYTg5YjQyYmMyZjhmYjY1OWYyYzQzMDUyOGEKFAjo5IrYFxCPh9ifBhgYIAw4CEAFGgJobCIgZDc1YzYwMjVkZDVmNGUzZDY1OGJlMzdhYWQ1ZjkxYjU; ssid_ucp_v1=1.0.0-KGQ4Nzc4MjZiYWI1NWRmYTg5YjQyYmMyZjhmYjY1OWYyYzQzMDUyOGEKFAjo5IrYFxCPh9ifBhgYIAw4CEAFGgJobCIgZDc1YzYwMjVkZDVmNGUzZDY1OGJlMzdhYWQ1ZjkxYjU; __ac_nonce=063f604bf003948a3bad9; __ac_signature=_02B4Z6wo00f01SciCjgAAIDARCjKUZOPEREnAg6AACpBKsMzhMS3tmVlQF6xo9y4lk1.8f3BOblIBNOWtyn2ZteVVEle52JVejsr-gjz52x7fFDNHLhhWPSptiT342agtlzTUnp1SO2LBVZxb3; ixigua-a-s=3; odin_tt=5529039b6eb72ae52e2705d0db550fc06655c83bdbe61914be61c42152989c387a55ad2fa056636bfd1f880a86407f88; tt_scid=ORPDc-M6KS5eOmTgjaUrefaIL0yzO7y.HdHVQAZXahd0wmCZXjYo6rBN9gfC3xYF3559; ttwid=1%7CiWx9zpr2eLSL5pxwfW7PdpTasAnL2Tszm5jFlS0A_ac%7C1677067476%7C2d4446661479733452a7b0217ff6d0c80645ddd3f9f9e85f43547870d43654da; msToken=cSgsxZfj-6sePYrKBxT8cLGTA9Fe4h9FVEyqx2na-t6-TyoXHDL3Q_CQIxRvs9MrWsKeXvTH9OeSdsfiVqYu48Qcw-sEg2hF6sThIHy2b9J1L2mNumIx',
    'referer': 'https://www.ixigua.com/7188507369205301794?logTag=2bb3393d5b417ff0f1fa',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
}
url = 'https://www.***.com/7188507369205301794?logTag=2bb3393d5b417ff0f1fa'
response = requests.get(url=url, headers=headers)
# 我这里只爬取了一个内容,我还录制了详细的视频讲解
# 直接在这个扣裙 708525271 拿视频和代码。
复制代码

 

获取数据

response.encoding = "utf-8"
html_data = response.text

 

解析数据

复制代码
json_str = re.findall('window._SSR_HYDRATED_DATA=(.*?)</script>', html_data)[0]
# 字典 undefined
json_str = json_str.replace('undefined', 'null')
json_data = json.loads(json_str)
video_list = json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['normal']['video_list']
num = len(list(video_list.keys()))
main_url = video_list.get(f'video_{num}').get("main_url")
video_url = base64.b64decode(main_url).decode()
print(video_url)
复制代码

 

保存视频/音频

复制代码
video_data = requests.get(video_url, headers=headers).content
with open('1.mp4', mode='wb') as f:
    f.write(video_data)
audio_data = requests.get(audio_url).content
with open(f'{title}.mp3', mode='wb') as f:
    f.write(audio_data)
ffmpeg = r'ffmpeg -i ' + title + '.mp4 -i ' + title + '.mp3 -acodec copy -vcodec copy ' + title + '-out.mp4'
subprocess.run(ffmpeg)
os.remove(f'{title}.mp3')
os.remove(f'{title}.mp4')
复制代码

 

最后

好了,今天的分享就到这里,喜欢的话记得点赞收藏,咱们下次见!

 

出处:https://www.cnblogs.com/hahaa/p/17148436.html

相关教程