本帖最后由 Traitor 于 2021-7-25 14:56 编辑
话不多说直接上代码.
第一段是使用的ALAPI的接口.
第二段代码是使用B站官方接口.
[Python] 纯文本查看 复制代码
import requests
api = "https://v2.alapi.cn/api/bilibili/cover"
data = {
"c": "",
"token": "VJF3R1rR5A7PCxyK"
}
def user_input():
print("请输入视频地址:")
url = input()
data["c"] = url
def get_img():
result = requests.get(api, params=data).json()
img_url = result["data"]["cover"]
img_title = result["data"]["title"]
img = img_title + ", " + img_url
return img
while True:
user_input()
try:
get_img()
print("是否要保存到文件内? 按回车键保存/输入任意键打印")
if input() == "":
with open("封面地址.txt", "a", encoding="UTF-8") as f:
f.write(get_img() + "\n")
print("保存成功!")
break
else:
print(get_img())
except TypeError:
print("\n请输入正确的视频地址!")
[Python] 纯文本查看 复制代码
import requests
import re
header = {
"User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/91.0.4472.164 Safari/537.36",
}
api = "https://api.bilibili.com/x/web-interface/view?bvid="
print("请输入带有BV的视频链接!")
url = input()
regex = re.compile("\\w+")
result = regex.findall(url)
bvid = result[5]
response = requests.get(api + bvid, headers=header).json()
img_title = response["data"]["title"]
img_url = response["data"]["pic"]
img = img_title + ", " + img_url
print("是否要保存到文件内? 按回车键保存/输入任意键打印")
if input() == "":
with open("封面地址.txt", "a", encoding="UTF-8") as f:
f.write(img + "\n")
print("保存成功!")
else:
print(img)
|