You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

import requests # 导入requests库用于发送HTTP请求
def bvid2cid(bvid):
url = f"https://api.bilibili.com/x/player/pagelist?bvid={bvid}" # API请求的URL
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
}
try:
response = requests.get(url, headers=headers) # 发送GET请求
response.raise_for_status() # 检查请求是否成功,抛出异常(如果请求失败)
data = response.json() # 解析返回的JSON数据
if 'data' in data and len(data['data']) > 0:
cid = data['data'][0]['cid'] # 提取第一个页面的cid
return cid # 返回cid
else:
raise ValueError("Data not found in response") # 如果数据不存在,抛出异常
except requests.RequestException as e:
print(f"Request error for bvid {bvid}: {e}") # 处理请求异常
except ValueError as e:
print(f"Value error for bvid {bvid}: {e}") # 处理数据解析异常
return None # 如果发生异常返回None
def main():
with open('bvid.txt', 'r') as file:
bvids = file.read().splitlines() # 读取bvid列表文件每行一个bvid
with open('cid.txt', 'w') as file:
for bvid in bvids:
cid = bvid2cid(bvid) # 获取bvid对应的cid
if cid:
file.write(f'{cid}\n') # 写入cid到文件
else:
file.write(f'{bvid}: Error fetching cid\n') # 如果获取cid失败记录错误信息
if __name__ == '__main__':
main() # 执行主函数