This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathbilibili.py
52 lines (43 loc) · 1.54 KB
/
bilibili.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
import re
import requests
def get(url: str) -> dict:
"""
imgs、videos
"""
data = {}
headers = {
"user-agent":
"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
"Referer": "https://www.bilibili.com/",
}
av_number_pattern = r'(BV[0-9a-zA-Z]*)'
cover_pattern = r"readyPoster: '(.*?)',"
video_pattern = r"readyVideoUrl: '(.*?)',"
title_pattern = r'title":"(.*?)",'
av = re.findall(av_number_pattern, url)
if av:
av = av[0]
else:
data["msg"] = "链接可能不正确,因为我无法匹配到av号"
return data
url = f"https://www.bilibili.com/video/{av}"
with requests.get(url, headers=headers, timeout=10) as rep:
if rep.status_code == 200:
cover_url = re.findall(cover_pattern, rep.text)
if cover_url:
cover_url = cover_url[0]
if '@' in cover_url:
cover_url = cover_url[:cover_url.index('@')]
data["imgs"] = ['https:' + cover_url]
video_url = re.findall(video_pattern, rep.text)
title_text = re.findall(title_pattern, rep.text)
if video_url:
video_url = video_url[0]
data["videos"] = [video_url]
if title_text:
data["videoName"] = title_text[0]
else:
data["msg"] = "获取失败"
return data
if __name__ == "__main__":
print(get(input("url: ")))