python爬取微博视频

本文介绍了如何使用requests库通过带有cookie的POST请求访问WeiboTV的component接口,以获取特定视频的URL并下载视频文件。
摘要由CSDN通过智能技术生成

思路

https://weibo.com/tv/show/1034:5028532510982210?mid=5028533009124778
查看原地址,发现是由component接口post请求携带参数 获取的 需要增加cookie

代码

import requests
import re


index_url = 'https://weibo.com/tv/show/1034:5028532510982210?mid=5028533009124778'
vid = re.findall('show/1034:(.*?)\\?mid', index_url)[0]
# print(vid)
url = 'https://weibo.com/tv/api/component?page=%2Ftv%2Fshow%2F1034%3A' + vid

headers = {
    'cookie': 'SINAGLOBAL=3560693377508.3193.1713855669164; _s_tentry=-; Apache=9755286762859.102.1714466966704; ULV=1714466966826:2:2:1:9755286762859.102.1714466966704:1713855669314; XSRF-TOKEN=KVYInRqkd3FtdBHDNdckWm2L; SUB=_2A25LNMR0DeRhGeNI4lMT-S7Kzz6IHXVoSFm8rDV8PUNbmtANLXj5kW9NSA860p7dTow29exd-1zv-hZxFn1Yp2ey; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW8qZPBvUAbCSPcfg7N1Qew5JpX5KzhUgL.Fo-c1K2E1K5cShz2dJLoI0YLxKnL1KeL1-BLxK-L1hqLBo5LxK.LBo.LB.qLxKML1-2L1hBLxK-LBo5L12qLxK-L122LBK.LxKML1K5L1hqt; ALF=02_1717059877; WBPSESS=SUDPZ91ded6LcROjUSEdouFprLPD4fR6BJgU7TSlB_jLhYzoSypwOhEsfJtbBS0jTwLYPw_vxHNbH3sLFpMaVQV-F2CNHrbG44aGUWnzihHGKVficbxB78fIreoWiG8QH6l3eqclp1zar83xgk63cw==',
    'referer': 'https://weibo.com/tv/show/1034:4694803645136959?mid=4694805986412000',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3880.400 QQBrowser/10.8.4554.400',
}

# data = {'data': '{"Component_Play_Playinfo":{"oid":"1034:4711490650046514"}}'}

data = {'data': '{"Component_Play_Playinfo":{"oid":"1034:' + vid + '"}}'}

json_data = requests.post(url=url, data=data, headers=headers).json()
# print(json_data)

video_url = 'http:' + json_data['data']['Component_Play_Playinfo']['urls']['高清 1080P']
print(video_url)

content = requests.get(url=video_url, headers=headers).content

with open('2.mp4', mode='wb') as f:
    f.write(content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值