import requests def bvid2cid(bvid): url = f"https://api.bilibili.com/x/player/pagelist?bvid={bvid}" 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) response.raise_for_status() # 检查请求是否成功 data = response.json() if 'data' in data and len(data['data']) > 0: cid = data['data'][0]['cid'] return 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 def main(): with open('bvid.txt', 'r') as file: bvids = file.read().splitlines() with open('cid.txt', 'w') as file: for bvid in bvids: cid = bvid2cid(bvid) if cid: file.write(f' {cid}\n') else: file.write(f'{bvid}: Error fetching cid\n') if __name__ == '__main__': main()