/
haokan.py
53 lines (48 loc) · 2.12 KB
/
haokan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding:utf-8 -*-
import requests
import json
import re
# 好看视频解析入口
class Bili(object):
def __init__(self, bv):
self.bv = bv
def get_video(self):
url = self.bv
if len(url) >= 23:
base_url = url
else:
base_url = "https://m.bilibili.com/video/" + str(self.bv)
headers = {
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
}
try:
response = requests.get(url=base_url, headers=headers, timeout=5)
if response.status_code == 200:
pattern = re.compile("options = \{(.*?)\}", re.S)
# res ="{"+ re.findall(pattern,response.text)[0]+"}"
try:
res = re.findall(pattern, response.text)[0]
aid = re.findall("aid: (.*?),", res)[0]
cid = re.findall("cid: (.*?),", res)[0]
readyDuration = re.findall("readyDuration: (.*?),", res)[0]
bvid = re.findall("bvid: '(.*?)',", res)[0]
readyPoster = re.findall("readyPoster: '(.*?)',", res)[0]
readyVideoUrl = re.findall("readyVideoUrl: '(.*?)',", res)[0]
rows = {
"aid": aid,
"bvid": bvid,
"cid": cid,
"readyPoster": "https:" + readyPoster,
"readyVideoUrl": "https:" + readyVideoUrl,
"readyDuration": readyDuration
}
return json.dumps(rows, ensure_ascii=False)
except Exception as e:
return "暂无相关数据,请检查相关数据:"+str(e)
else:
return "暂无相关数据,请检查相关数据"
except Exception as e:
return e
if __name__ == '__main__':
bili = Bili("https://haokan.baidu.com/v?vid=11231414361411904045")
print(bili.get_video())