# 开发时间:2022/2/22 10:38 import requests import json import os table = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF' tr = {} for i in range(58): tr[table[i]] = i s = [11, 10, 3, 8, 4, 6] xor = 177451812 add = 8728348608 samename = {} samefan = {} def dec(x): # decoding,BV转化为AV号 r = 0 for i in range(6): r = r+tr[x[s[i]]]*58**i return (r-add)^xor def getComment(Bvid, page): Avid = dec(Bvid) url = "https://api.bilibili.com/x/v2/reply?pn=" + str(page) + "&type=1&oid=" + str(Avid) + "&sort=2" data = requests.get(url) content = json.loads(data.text) return content['data']['replies'] BVID = "BV1P5411Z7YU" page = 1 # 需要爬的评论区页数,1页有20条评论 with open('评论.txt', 'w', encoding = 'utf-8') as f: for pg in range(1, int(page) + 1): commonComment = getComment(BVID, pg) commentLength = len(commonComment) for index in range(commentLength): if commonComment[index]['member']['user_sailing']['cardbg']: f.write('第'+str(pg)+'页第'+str(index+1)+'条'+'\t'+commonComment[index]['member']['uname']+'\t'+commonComment[index]['member']['user_sailing']['cardbg']['name']+':\n') if commonComment[index]['member']['user_sailing']['cardbg']['name'] in samefan: samefan[commonComment[index]['member']['user_sailing']['cardbg']['name']] += 1 else: samefan.setdefault(commonComment[index]['member']['user_sailing']['cardbg']['name'], 1) else: f.write('第' + str(pg) + '页第' + str(index + 1) + '条' + '\t' + commonComment[index]['member']['uname'] + ':\n') if commonComment[index]['member']['uname'] in samename: samename[commonComment[index]['member']['uname']]+=1 else: samename.setdefault(commonComment[index]['member']['uname'], 1) f.write(commonComment[index]['content']['message']+'\n') with open('评论详情.txt', 'w', encoding = 'utf-8') as f: for i in samename.keys(): #print(i+'\t', samename[i]) f.write(i+'\t'+str(samename[i])+'\n') with open('牌子详情.txt', 'w', encoding='utf-8') as f: for i in samefan.keys(): #print(i+'\t', samename[i]) f.write(i+'\t'+str(samefan[i])+'\n')
原代码来源于b站的一个up的视频,这里是根据那个代码小小改动了一下