import requests
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import csv
from fake_useragent import UserAgent
ua = UserAgent()
header = {"User-Agent": ua.chrome, "Cookie": ""}
comments = []
#测试
filename='大笑江湖.txt'
id=63441039
p=0
f=open(filename,mode='w',encoding='GBK',errors='ignore')
csvwrite=csv.writer(f)
for page in range(1, 100):
url=("https://api.bilibili.com/x/v2/reply?jsonp=jsonp&type=1&oid={}&sort=2&pn=".format(id))+str(page)
html = requests.get(url, headers=header)
data = html.json()
print(url)
# print(data)
if data['data']['replies']!=None:
shu=len(data['data']['replies'])
for shus in range(0,shu):
p=p+1
print(str(p)+"------"+(data['data']['replies'][shus]['member']['uname'])+"--------"+(data['data']['replies'][shus]['content']['message']))
name=data['data']['replies'][shus]['member']['uname']
mess=data['data']['replies'][shus]['content']['message']
comments.append([name,mess])
else:
break
for k in range(0,len(comments)-1):
csvwrite.writerow(comments[k])
附件:利用某站评论的txt文件
id:F12查找oid