|
| 1 | +import requests |
| 2 | +import datetime |
| 3 | +import re |
| 4 | +import pandas as pd |
| 5 | +import random |
| 6 | +import time |
| 7 | +import progressbar |
| 8 | + |
| 9 | +def json_bvid(bvid, like, coin, favorite, share, reply, danmaku, his_rank): |
| 10 | + '''获取单个视频的数据''' |
| 11 | + url1 = 'https://api.bilibili.com/x/web-interface/view?bvid={}'.format(bvid) |
| 12 | + json_bvid = requests.get(url1) |
| 13 | + like.append(json_bvid.json()[ u'data'][u'stat'][u'like']) |
| 14 | + coin.append(json_bvid.json()[ u'data'][u'stat'][u'coin']) |
| 15 | + favorite.append(json_bvid.json()[ u'data'][u'stat'][u'favorite']) |
| 16 | + share.append(json_bvid.json()[ u'data'][u'stat'][u'share'] ) |
| 17 | + reply.append(json_bvid.json()[ u'data'][u'stat'][u'reply']) |
| 18 | + danmaku.append(json_bvid.json()[ u'data'][u'stat'][u'danmaku'] ) |
| 19 | + his_rank.append(json_bvid.json()[ u'data'][u'stat'][ u'his_rank']) |
| 20 | + |
| 21 | + |
| 22 | +url = 'https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3' |
| 23 | +headers={"Host": |
| 24 | +"api.bilibili.com", |
| 25 | +"User-Agent": |
| 26 | +"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0", |
| 27 | +"Referer": |
| 28 | +"https://space.bilibili.com/96654548/video", |
| 29 | +"Accept": |
| 30 | +"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", |
| 31 | +"Accept-Language": |
| 32 | +"zh-CN,zh;q=0.9,en;q=0.8", |
| 33 | +"Accept-Encoding": |
| 34 | +"gzip, deflate, br", |
| 35 | +"Connection": |
| 36 | +"keep-alive", |
| 37 | +"Cache-Control":"max-age=0", |
| 38 | +"Upgrade-Insecure-Requests":"1", |
| 39 | +"Cookie": |
| 40 | +"_uuid=500B1F19-1AD8-2AF1-FA34-A76D73F5C98913672infoc; buvid3=2EF6859F-2258-4D84-81BF-165ACE9CD69F53934infoc" |
| 41 | +} |
| 42 | +proxies={'HTTP': 'HTTP://122.242.96.30:808', 'HTTPS': 'HTTPS://122.242.96.30:808'}#免费IP地址*http://www.xicidaili.com* |
| 43 | +r = requests.get(url) |
| 44 | +s = re.findall(r'<li class=\"rank-item\"><div class=\"num\">(\d+)<\/div><div class=\"content\"><div class=\"img\"><a href=\"(.*?)\" target=\"_blank\"><div class=\"lazy-img cover\"><img alt=\"(.*?)\" src=.*?<i class=\"b\-icon play\"><\/i>(.*?)<\/span><span class=\"data-box\"><i class=\"b-icon view\"><\/i>(.*?)<\/span><a target=\"_blank\" href=\"(.*?)\"><span class=\"data-box\"><i class=\"b-icon author\"><\/i>(.*?)<\/span><\/a>',r.content) |
| 45 | +head = ['ranking','url_videos_bilibili','title','played','views','up_space','author'] |
| 46 | +df = pd.DataFrame(s, columns = head) |
| 47 | +like, coin, favorite, share, reply, danmaku, his_rank = [],[],[],[],[],[],[] |
| 48 | +T_likes, T_views, followers, following = [],[],[],[] |
| 49 | +widgets = ['Progress: ',progressbar.Percentage(), ' ', progressbar.Bar('#'),' ', progressbar.Timer(), |
| 50 | + ' ', progressbar.ETA(), ' ', progressbar.FileTransferSpeed()] |
| 51 | +pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(s)).start() |
| 52 | +for i in range(len(s)): |
| 53 | + bvid = s[i][1][-12:] |
| 54 | + json_bvid(bvid, like, coin, favorite, share, reply, danmaku, his_rank) |
| 55 | + uuid = re.findall(r'(\d+)',s[i][5]) |
| 56 | + upstat = requests.get('https://api.bilibili.com/x/space/upstat?mid={}'.format(uuid[0]),headers = headers,proxies = proxies) |
| 57 | + stat = requests.get('https://api.bilibili.com/x/relation/stat?vmid={}'.format(uuid[0])) |
| 58 | + T_likes.append(upstat.json()[u'data'][u'likes']) |
| 59 | + T_views.append(upstat.json()[u'data'][u'archive'][u'view']) |
| 60 | + followers.append(stat.json()[u'data'][u'follower']) |
| 61 | + following.append(stat.json()[u'data'][u'following']) |
| 62 | + |
| 63 | + pbar.update( i + 1) |
| 64 | +df['点赞'] = like |
| 65 | +df['投币'] = coin |
| 66 | +df['收藏'] = favorite |
| 67 | +df['转发'] = share |
| 68 | +df['评论'] = reply |
| 69 | +df['弹幕'] = danmaku |
| 70 | +df['单日全站排名'] = his_rank |
| 71 | +df['总获赞数'] = T_likes |
| 72 | +df['粉丝数'] = followers |
| 73 | +df['关注数'] = following |
| 74 | +df['总播放数'] = T_views |
| 75 | + |
| 76 | +d = datetime.datetime.today() |
| 77 | +df.to_csv('/Users/jin/Documents/bilibili/data_rank_bilibili_{}.csv'.format(d), encoding='utf-8') |
0 commit comments