使用Python模拟登陆bilibili(过滑动验证)
首先需要验证码识别平台。 看代码。官方文档上面有使用说明,可以琢磨一下。For free
和我之前的差不多,练练手
https://www.52pojie.cn/thread-1455857-1-1.html
----------------------------------------------------------------
没吃饭就抽个空出来把这个代码完善,刚刚完善完,就准备发吾爱来供大家参考
验证码识别失败的可以套try catch 循环什么的。 验证登录成功有什么信息即可(例如:个人中心)
直接给大家放成品好了。
打码平台导入进来先
#!/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__':
# 用户中心>>软件ID 生成一个替换 96001
chaojiying = Chaojiying_Client('username', 'password', '96001')
# 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# im 图片的字节
im = open('cap1.png', 'rb').read()
print(chaojiying.PostPic(im, 9101))
# {'err_no': 0, 'err_str': 'OK', 'pic_id': '1144813466470700019', 'pic_str': '185,71', 'md5': '2ce2a82ac54a1f0242cb896abb1c6c25'}
正菜
import time
from PIL import Image
from selenium.webdriver import Chrome
from selenium.webdriver import ActionChains
from io import BytesIO
import chaojiying
browse = Chrome()
# 定义好超级鹰的账号密码
chaojiying = chaojiying.Chaojiying_Client('username', 'password', '918082')
# 可用这个方法去获取图片
def crop_image(image_file_name):
# 截图
# 定位你需要的元素 找到他
img = browse.find_element_by_xpath("//div[@class='geetest_slicebg geetest_absolute']")
# ver_img = browse.find_element_by_xpath("//div[@class='geetest_slicebg geetest_absolute']")
location = img.location
size = img.size
print("图片的位置", location)
print("图片的大小", size)
# real location is 1071*391
# get location is 1078*283
top, buttom, left, right = location['y'], location['y'] + size['height'], location["x"], location["x"] + size[
"width"]
print("verify code location is : ", left, top, right, buttom)
screenshot = browse.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
#intand tuple
capt = screenshot.crop((int(left), int(top), int(right), int(buttom)))
capt.save(image_file_name)
return capt
def login():
url = "https://passport.bilibili.com/login"
browse.get(url)
# user and password of bilibili
username = "123456"
password = "54321"
# 最大化
browse.maximize_window()
# find the name,password and login element and input data for click
name_ele = browse.find_element_by_xpath("//input[@id='login-username']")
pass_ele = browse.find_element_by_xpath("//input[@id='login-passwd']")
login_ele = browse.find_element_by_xpath("//a[@class='btn btn-login']")
name_ele.send_keys(username)
pass_ele.send_keys(password)
login_ele.click()
# 鼠标放在滑动验证码的最左边
time.sleep(5)
# find slider element
slider = browse.find_element_by_xpath("//div[@class='geetest_slider_button']")
# 事件链
ActionChains(browse).move_to_element(slider).perform()
# 点了之后休息三秒
time.sleep(3)
# get verify code
img1 = browse.find_element_by_xpath("//div[@class='geetest_slicebg geetest_absolute']")
# get picture of gap ==> need ChaoJiYing
dic = chaojiying.PostPic(img1.screenshot_as_png, 9101)
# 从返回的值里面找到需要点击的下标
res = dic['pic_str']
print(res)
# 分割 这里大家可以自己输出一下 就知道了
# 28,110
temp = res.split(",")
# adjust data==> CJY return wrong dates,we should adjust it.According to official documents
# 9101 坐标选一,返回格式:x,y 15
x = int(temp) - 10
y = int(temp)
print("x is {}, y is {}".format(x, y))
# 移动到某个区域 然后再进行点击
# 然后找到区域 进行偏移
# 是事件链
# print("接下来的事件")
# 按住不释放 ==> 拖动
ActionChains(browse).click_and_hold(slider).perform()
# only xoffset
ActionChains(browse).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(2)
# 释放 即可登录成功
ActionChains(browse).release().perform()
time.sleep(10)
if __name__ == '__main__':
login()
QingYi. 发表于 2021-6-22 10:15
是什么浏览器就导入什么内核 Firefox也可以
需要内核就证明有些算法你这边没模拟出来,借浏览器才完成了 侃遍天下无二人 发表于 2021-6-22 10:17
需要内核就证明有些算法你这边没模拟出来,借浏览器才完成了
???我这个就借助浏览器来做事的啊 学习了 楼主不错哦 看的我都想学python 了 试试selenium的click,,期待你的新作{:301_1004:} GiaoMan-wei 发表于 2021-6-21 19:16
试试selenium的click,,期待你的新作
好的 马上在91上面更新 厉害了大佬,回头试试 好家伙,一个比一个厉害 得抓紧学习python了
python在手,天下我有 还需要chrome内核才能运行吗 可以直接cookie登录吧
页:
[1]
2