From f8f30430bb1ef18747f713fe359f27d5e8e031cb Mon Sep 17 00:00:00 2001 From: pff5ls68k <1822218970@qq.com> Date: Mon, 16 Sep 2024 10:48:46 +0800 Subject: [PATCH] Project --- cid_search.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 cid_search.py diff --git a/cid_search.py b/cid_search.py new file mode 100644 index 0000000..cbd1583 --- /dev/null +++ b/cid_search.py @@ -0,0 +1,39 @@ +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() # 执行主函数