b站每天自动投币三连,轻松获取60经验值

前言

今天在玩b站的时候发现自己快要到6级大佬了,小本本计算一下,也就差个5千多经验…

可是自己白嫖习惯了,也不怎么投币,所以等级升的非常慢,和我一起入学的都已经是6级大佬了

看了一下b站的升级任务,每天投币是不能少的,刚好最近对selenium玩的比较嗨,就想做一个每天自动帮我投币的py

想要的功能

1.自动登录b站

2.随机观看几个视频并且帮我投币

我写的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
import time
from PIL import Image
import json
import random


class CrackSlider():
def __init__(self):
super(CrackSlider, self).__init__()
# 初始 url
self.start_url = 'https://www.bilibili.com/'
# 登录 url
self.login_url = 'https://passport.bilibili.com/login'
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来
self.options = webdriver.ChromeOptions()
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.options.add_experimental_option('useAutomationExtension', False)
# 设置无头模式,就是不显示浏览器,测试时可以注释掉
self.options.add_argument('--headless')

self.driver = webdriver.Chrome(options=self.options)
self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
self.driver.set_window_size(1366, 768)
# self.driver.maximize_window() # 全屏
# 等待加载元素,最多10s
self.wait = WebDriverWait(self.driver, 10)

# 获取并保存cookie
def save_cookie(self):
self.driver.get(self.login_url)
# 网页元素截图
usercode_img = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located(
(By.CLASS_NAME, 'qrcode-img')
)
)
self.driver.save_screenshot('screenshot.png') # 截全图
left = usercode_img.location['x']
top = usercode_img.location['y']
rigth = usercode_img.location['x'] + usercode_img.size['width']
bottom = usercode_img.location['y'] + usercode_img.size['height']

im = Image.open('screenshot.png')
im = im.crop((left, top, rigth, bottom)) # 对浏览器截图进行裁剪
im.save('vcode.png')
print('请扫码登录')
time.sleep(2)
# 打开图片扫码登录
img=Image.open('vcode.png')
img.show()
# 检测是否登录成功
current_url = self.login_url
while current_url:
if self.driver.current_url != current_url:
current_url = False
time.sleep(3)
print('登录成功,正在保存cookies')
cookies = self.driver.get_cookies()
json_cookies = json.dumps(cookies)
with open('cookies_blbl.json', 'w') as f:
f.write(json_cookies)

# 读取cookies
def add_cookie(self):
self.driver.delete_all_cookies()
try:
with open('cookies_blbl.json', 'r', encoding='utf-8') as f:
list_cookies = json.loads(f.read())
# print(list_cookies)
for i in list_cookies:
self.driver.add_cookie(i)
except:
self.save_cookie()

# 登录
def logo(self):
print('开始登录中...')
self.driver.get(self.login_url)
self.add_cookie()
self.driver.get(self.start_url)
time.sleep(3)
# 检测是否登录成功
if self.driver.current_url != self.start_url:
self.save_cookie()
print('使用cookies登录成功')

# 随机看视频
def look_b(self):
sp_list = []
self.driver.get('https://www.bilibili.com/v/popular/rank/all')
time.sleep(3)
sps = self.driver.find_elements_by_xpath('//ul[@class="rank-list"]/li/div[2]/div[2]/a')
for sp in sps:
sp_url = sp.get_attribute('href')
# print(sp_url)
sp_list.append(sp_url)
# 随机观看6个视频
for i in range(7):
# 随机数
sjh = random.randint(0, len(sp_list) - 1)
self.driver.get(sp_list[sjh])
print('开始前往{}看视频三连'.format(sp_list[sjh]))
# 随机时间三连
time.sleep(random.randint(5,10))
# 点赞按钮
spot = self.driver.find_element_by_id('arc_toolbar_report').find_element_by_class_name('like')
# 鼠标左键按下不动
ActionChains(self.driver).click_and_hold(spot).perform()
time.sleep(5)
# 释放左键
ActionChains(self.driver).release(spot).perform()
# 观看时长
time.sleep(random.randint(30, 60))


if __name__ == '__main__':
c = CrackSlider()
# 登录
c.logo()
# 随机看视频
c.look_b()

使用方法

1
2
3
4
5
安装 python3.7+
安装 selenium等一系列的库,配置好谷歌浏览器的selenium插件,百度就行
第一次运行需要自己扫码登录
win7设置任务计划,每天运行时间自己设置,电脑不能关机休眠,其他系统自行设置
我是用公司的电脑,每天12点吃饭的空闲时间自动运行

后记

github有好多云函数运行的项目,不需要电脑,可惜我不会用,就只能用这个自行车了!