python--requests--爬取bilili评论区获取幸运观众ID
#需要导入的模块 import requests import json import pprint import random import time #请求链接 r = requests.get('https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=46445761&sort=0&_=1553736731025') data = json.loads(r.text) #评论页数计算 pprint.pprint(data['data']['page']['count']//20+1) user_map={} total_pages = data['data']['page']['count']//20+1 while True:#与time.sleep(500)作用组成5秒就重新请求一次 #逐步遍历抽取 for page in range(40,total_pages+1): r = requests.get('https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn={}&type=1&oid=46445761&sort=0&_=1553736731025'.format(page)) data = json.loads(r.text) #楼中楼逐步遍历抽取 for i in data['data']['replies']: #查看pprint.pprint(i['replies']) user_map[i['member']['mid']]=i['member']['uname'] #去掉没有评论 if i['replies']!=None: for j in i['replies']: #查看pprint.pprint(j['member']) user_map[j['member']['mid']] = j['member']['uname'] #打印抽取数值 pprint.pprint(user_map) lucky_user_i = random.choice(list(user_map)) print('幸运观众ID:{},昵称:{}'.format(lucky_user_i,user_map[lucky_user_i])) time.sleep(1)