import requests
uid=str(input("输入查询用户的UID:"))
url01="https://api.bilibili.com/x/relation/stat?vmid="+uid+"&jsonp=jsonp"
header01={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"}
res01=requests.get(url=url01,headers=header01)
p=res01.text.find("follower")
fans_number=int(res01.text[p+10:len(res01.text)-2])
fans_list=[]
if fans_number>250:
print("该用户粉丝数共",fans_number,"个,目前只能查询到前250位粉丝信息如下:")
for i in range(0,5):
url="http://api.bilibili.com/x/relation/followers?vmid="+uid+"&pn="+str(i+1)
header=header01
res=requests.get(url=url,headers=header)
fans_data=str(res.text)
num=fans_data.count("uname")
for i in range(0,num):
a=fans_data.find("uname")
b=fans_data.find("face")
c=fans_data.find("track_id")
fans_list.append(fans_data[a+8:b-3])
fans_data=str(fans_data[c+10:])
else:
print("该用户粉丝数共",fans_number,"个,所有粉丝信息如下:")
pagemax=int(fans_number/50)+1
for i in range(0,pagemax):
url="http://api.bilibili.com/x/relation/followers?vmid="+uid+"&pn="+str(i+1)
header=header01
res=requests.get(url=url,headers=header)
fans_data=str(res.text)
num=fans_data.count("uname")
for i in range(0,num):
a=fans_data.find("uname")
b=fans_data.find("face")
c=fans_data.find("track_id")
fans_list.append(fans_data[a+8:b-3])
fans_data=str(fans_data[c+10:])
print(fans_list,len(fans_list))
代码运行实例:
①>250粉丝:uid=481701670
②≤250粉丝:uid=628602316