Skip to content

Commit 4ce8d35

Browse files
committed
bilibili_ranking_video
get the 100 rank data of bilibili
1 parent 7a681c0 commit 4ce8d35

9 files changed

Lines changed: 148 additions & 1 deletion

.DS_Store

2 KB
Binary file not shown.

Leetcode/leetcode-69-sqrtx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#coding:UTF-8
22
#Author: Toryun
3-
#Date: 2020-04-17 16:41:00
3+
#Date: 2020-04-19 16:41:00
44
#https://leetcode-cn.com/problems/sqrtx/
55

66
from __future__ import division

bilibili/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# bilibili 每日全站排行榜
2+
3+
[每天早上十点更新]
4+
5+
## 数据表头
6+
7+
| | | | | | | | | | | | | | | | | | |
8+
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
9+
|ranking| url_videos_bilibili |title| played |views |up_space |author | 点赞 |投币 |收藏 |转发 |评论 |弹幕 |单日全站排名 |总获赞数| 粉丝数 |关注数 |总播放数|
10+
11+
## 说明
12+
13+
### 运行环境
14+
15+
Mac 和 Python2.7.15
16+
17+
直接点击bilibili_rank.sh,它会把执行日志和数据保存在同目录下,可以修改保存路径,数据保存格式为csv

bilibili/bilibili_rank.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# 记录一下开始时间
4+
echo `date` >> /Users/jin/Documents/GitHub/Python_repository/bilibili/log &&
5+
# 进入helloworld.py程序所在目录
6+
cd /Users/jin/Documents/GitHub/Python_repository/bilibili &&
7+
# 执行python脚本(注意前面要指定python运行环境/usr/bin/python,根据自己的情况改变)
8+
/usr/bin/python bilibili_ranking_data_everyday.py
9+
#运行完成
10+
echo 'finish' >> /Users/jin/Documents/GitHub/Python_repository/bilibili/log
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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')

bilibili/log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2020年 4月20日 星期一 06时48分50秒 CST
2+
Mon Apr 20 13:59:24 CST 2020

bilibili/log.err

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Traceback (most recent call last):
2+
File "bilibili_ranking_data_everyday.py", line 1, in <module>
3+
import requests
4+
ImportError: No module named requests
5+
/Users/jin/Documents/GitHub/Python_repository/bilibili/bilibili_rank.sh: line 10: /Users/demo/log: No such file or directory

bilibili/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests==2.21.0
2+
pandas==0.24.2
3+
progressbar==2.5

bilibili_ranking_data_everyday.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import requests
2+
import datetime
3+
import re
4+
import pandas as pd
5+
import random
6+
import time
7+
import json
8+
9+
url = 'https://www.bilibili.com/ranking?spm_id_from=333.851.b_7072696d61727950616765546162.3'
10+
r = requests.get(url)
11+
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)
12+
head = ['ranking','url_videos_bilibili','title','played','views','up_space','author']
13+
like, favorite, share, reply, danmaku, his_rank = [],[],[],[],[],[]
14+
for i in range(len(s)):
15+
bvid = s[i][1][-12:]
16+
url1 = 'https://api.bilibili.com/x/web-interface/view?bvid={}'.format(bvid)
17+
json_bvid = requests.get(url1)
18+
like.append( json_bvid.json()[ u'data'][u'stat'][u'like'])
19+
favorite.append( json_bvid.json()[ u'data'][u'stat'][u'favorite'])
20+
share.append( json_bvid.json()[ u'data'][u'stat'][u'share'])
21+
reply.append( json_bvid.json()[ u'data'][u'stat'][u'reply'])
22+
danmaku.append( json_bvid.json()[ u'data'][u'stat'][u'danmaku'])
23+
his_rank.append( json_bvid.json()[ u'data'][u'stat'][u'his_rank'])
24+
25+
df = pd.DataFrame(s, columns = head)
26+
df['点赞'] = like
27+
'收藏',
28+
'转发',
29+
'评论',
30+
'弹幕',
31+
'单日全站排名'] = , favorite, share, reply, danmaku, his_rank
32+
d = datetime.datetime.today()
33+
df.to_csv('/Users/jin/Documents/bilibili/data_rank_bilibili_{}.csv'.format(d), encoding='utf-8')

0 commit comments

Comments
 (0)