用python爬取腾讯视频代码
时间: 2023-09-12 16:10:23 浏览: 232
好的,以下是用 Python 爬取腾讯视频的代码:
```python
import requests
import json
url = "https://access.video.qq.com/user/auth_refresh"
params = {
"refer": "https://v.qq.com/",
"sdtfrom": "v1010",
"otype": "json",
"callback": "jQuery1910022431697752040718_1613457313315",
"_": "1613457313316"
}
# 发送 GET 请求,获取 Cookie
response = requests.get(url, params=params)
cookies = response.cookies
# 构造请求头
headers = {
"Referer": "https://v.qq.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
# 构造请求参数
vid = "i0032d0qztl"
params = {
"otype": "json",
"vid": vid,
"defn": "shd",
"platform": "10901",
"sdtfrom": "v1010",
"host": "v.qq.com"
}
# 发送 GET 请求,获取视频信息
url = f"https://h5vv.video.qq.com/getinfo?callback=&platform=10901&charge=0&vid={vid}&defn=shd&defnpayver=1&otype=json&ehost=https%3A%2F%2Fv.qq.com&sphls=0&sb=1&nocache=0&_rnd=1613457313325"
response = requests.get(url, headers=headers, params=params, cookies=cookies)
data = json.loads(response.text[response.text.find("{"):response.text.rfind("}")+1])
# 获取视频 url
url = data["vl"]["vi"][0]["ul"]["ui"][0]["url"]
fn = data["vl"]["vi"][0]["fn"]
fvkey = data["vl"]["vi"][0]["fvkey"]
url += fn + "?vkey=" + fvkey
print(url)
```
其中,`vid` 是腾讯视频中视频的唯一标识符,可以在视频的网页链接中找到。该代码通过发送两个 GET 请求获取视频信息以及视频地址,最后将视频地址输出。注意,需要使用浏览器开发者工具获取 Cookie,并将其传入请求头中。
阅读全文