bilibili.py 文件
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from PIL import Image
import random
from 项目.chaojiying import Chaojiying_Client
from selenium.webdriver import ActionChains
class BiliBili(object):
def __init__(self):
# 起始登录页url
self.url = 'https://passport.bilibili.com/login'
self.web = webdriver.Chrome()
def login(self):
# xpath定位输入框
self.web.find_element_by_xpath('//*[@id="login-username"]').send_keys('b站账号')
self.web.find_element_by_xpath('//*[@id="login-passwd"]').send_keys('b站密码', Keys.ENTER)
def save_img(self):
# 对当前页面进行截屏
self.web.save_screenshot('page.png')
# 定位验证码的位置
code_img_ele = self.web.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div')
location = code_img_ele.location # 验证码左上角的坐标x,y
size = code_img_ele.size # 验证码图片对应的长和宽
rangle = (
int(location['x'] * 1.25), int(location['y'] * 1.25), int((location['x'] + size['width']) * 1.25),
int((location['y'] + size['height']) * 1.25)
)
i = Image.open('./page.png')
code_img_name = './code.png'
# crop根据rangle元组内的坐标进行裁剪
frame = i.crop(rangle)
frame.save(code_img_name)
return code_img_ele
def narrow_img(self):
# 缩小图片
code = Image.open('./code.png')
small_img = code.resize((169, 216))
small_img.save('./small_img.png')
print(code.size, small_img.size)
def submit_img(self):
# 将验证码提交给超级鹰进行识别
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '913628') # 用户中心>>软件ID 生成一个替换 96001
im = open('small_img.png', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 9004)['pic_str'])
result = chaojiying.PostPic(im, 9004)['pic_str']
return result
def parse_data(self, result):
all_list = [] # 存储即将被点击的点的坐标 [[x1,y1],[x2,y2]]
if '|' in result:
list_1 = result.split('|')
count_1 = len(list_1)
for i in range(count_1):
xy_list = []
x = int(list_1[i].split(',')[0])
y = int(list_1[i].split(',')[1])
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
else:
print(result.split(',')[0])
print(result.split(',')[1])
x = int(result.split(',')[0])
y = int(result.split(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
return all_list
def click_codeImg(self, all_list, code_img_ele):
# 遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for l in all_list:
x = l[0] * 1.6
y = l[1] * 1.6
# move_to_element_with_offset移动到距某个元素(左上角坐标)多少距离的位置
ActionChains(self.web).move_to_element_with_offset(code_img_ele, x, y).click().perform()
time.sleep(random.random())
print('点击已完成')
time.sleep(random.random() * 2)
# 完成动作链点击操作后,定位确认按钮并点击
self.web.find_element_by_xpath('/html/body/div[2]/div[2]/div[6]/div/div/div[3]/a').click()
def user_space(self):
time.sleep(2)
# 跳转到用户页面
self.url = "https://space.bilibili.com/105113548/video"
self.web.get(self.url)
def dianzan(self):
all_list = self.web.find_elements_by_class_name('small-item')
# print(all_list)
for video in all_list:
# print(video)
# 点击进入视频
video.find_element_by_xpath('./a[1]').click()
# 跳转到倒数第一个窗口
self.web.switch_to.window(self.web.window_handles[-1])
time.sleep(2)
# 点赞
self.web.find_element_by_xpath('//*[@id="arc_toolbar_report"]/div[1]/span[1]/i').click()
# 关闭窗口
self.web.close()
# 回到新窗口
self.web.switch_to.window(self.web.window_handles[0])
time.sleep(1)
def run(self):
# url
# web
# get
self.web.get(self.url)
# 登陆
self.login()
time.sleep(1)
# 截图
code_img_ele = self.save_img()
# 处理图片
self.narrow_img()
# 将图片提交给超级鹰,获取返回值
result = self.submit_img()
# 解析返回值,将数据格式化
all_list = self.parse_data(result)
# 在页面验证码上完成点击操作
self.click_codeImg(all_list, code_img_ele)
# 跳转到用户页
self.user_space()
# 点赞
self.dianzan()
if __name__=='__main__':
bilibili = BiliBili()
bilibili.run()
chaojiying,py
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
# if __name__ == '__main__':
# chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '96001') #用户中心>>软件ID 生成一个替换 96001
# im = open('a.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()