一直想用python写一个程序帮我自动登陆B站,完成一些点击任务,懂的都懂 =v=

最近终于腾出时间来搞了,其实最难的部分就是中文验证码的识别。这个借助API接口也能轻松搞定。下面分享一下全部源码(前面是过程讲解,只需要全部源码的可以直接翻到最后):

首先是导入需要的库,定义需要的常量:

import os.path
import random
import time
from selenium import *
from selenium import webdriver
from PIL import Image
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import base64
import json
import requests
from selenium.webdriver import ActionChainsurl='https://passport.bilibili.com/login'
driver = webdriver.Chrome(executable_path=r"C:\Users\ASUS\AppData\Local\Google\Chrome\Application\chromedriver.exe")
driver.set_window_size(1100, 958)
bool=True

注意,上面的executable_path需要改成你自己 chromedriver.exe 的位置!chromedriver.exe是selenium驱动chrome游览器的必要组件,所以一定是需要的。

另外,window_size(1100, 958) 这里不要改,因为和后面的数字有对应关系,要改也可以,后面截图裁减图片的数字你也要对应修改。


while bool:driver.get(url)'''填写用户名和密码'''xpath='//*[@id="login-username"]'driver.find_element_by_xpath(xpath).send_keys('your_username')xpath='//*[@id="login-passwd"]'driver.find_element_by_xpath(xpath).send_keys('your_password')'''点击登录'''time.sleep(0.5)class_name='btn-login'driver.find_element_by_class_name(class_name).click()

这里 your_username 换成你的B站登陆用户名,your_password 换成你的登陆密码。

    '''创建文件保存验证码'''image_name=str(int(1000000 * time.time()))+'.png'if not os.path.exists("yzm_small"):os.mkdir("yzm_small")if not os.path.exists("yzm_large"):os.mkdir("yzm_large")'''等待验证码图像出现'''class_name='geetest_tip_img'try:element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,class_name)))except Exception as e:print(e)continue'''截取验证码图片,并分为大图和小图验证码'''time.sleep(0.5)driver.save_screenshot('yzm.png')'''这里计算屏幕的拉伸率,不同电脑的windows缩放比例会影响到截图的像素点位置'''width=Image.open('yzm.png').size[0]stretch_rate=width/1100print('当前屏幕的缩放比例为:'+str(stretch_rate))img = Image.open('yzm.png')cropped = img.crop((800*stretch_rate,246*stretch_rate,925*stretch_rate,283*stretch_rate))  # (left, upper, right, lower)cropped.save('yzm_small/'+image_name)cropped = img.crop((668.5*stretch_rate,287*stretch_rate,925*stretch_rate,547*stretch_rate))  # (left, upper, right, lower)cropped.save('yzm_large/'+image_name)

这里是获取验证码的关键步骤,我是通过游览器屏幕直接截取的,所以如果你要直接拿来用,一定要注意这个stretch_rate。不同的电脑不一样(屏幕拉伸率,可以在你的电脑设置里面看到),有可能造成截图验证码截不到正确的位置,所以我这里添加了stretch_rate修正过了,除非网站本身发生变动,否则截取到需要的验证码是没有任何问题的。

接下来就是最麻烦的中文验证码识别了。这里调用的是图灵验证码识别平台,这也是我全网唯一找到能够准确识别中文验证码的平台了。(这个不是人工打码,24小时都可以用)

在线图片验证码识别平台-图像验证码识别打码平台-图片验证码打码平台-图灵

官网网址:http://tulingcloud.com

选择中文验证码识别模型,找到自己需要的模型ID:

我们需要采用的是模型12和9,分别来识别验证码小图和大图:

API的调用方式就不赘述了,自己去看图灵验证码识别网站上已经写得很详细了,直接贴代码:

    '''使用图灵验证码识别平台,进行验证码识别''''''图灵验证码识别平台:  http://www.tulingtech.xyz/static/index.html  '''def tuling_api(username, password, img_path, ID):with open(img_path, 'rb') as f:b64_data = base64.b64encode(f.read())b64 = b64_data.decode()data = {"username": username, "password": password, "ID": ID, "b64": b64}data_json = json.dumps(data)result = json.loads(requests.post("http://www.tulingtech.xyz/tuling/predict", data=data_json).text)return result'''小图部分识别'''img_path = 'yzm_small/'+image_nameresult_small = tuling_api(username="你的图灵验证码识别平台账号", password="你的图灵验证码识别平台密码", img_path=img_path, ID="02156188")result_small=result_small['result']print(result_small)'''大图部分识别'''img_path = 'yzm_large/' + image_nameresult_large = tuling_api(username="你的图灵验证码识别平台账号", password="你的图灵验证码识别平台密码", img_path=img_path, ID="05156485")print(result_large)

最后使用selenium完成自动化点击中文汉字:

    class_name='geetest_tip_content'element = driver.find_element_by_class_name(class_name)try:for i in range(0, len(result_small)):result=result_large[result_small[i]]ActionChains(driver).move_to_element(element).move_by_offset(-70+int(result['X坐标值']/stretch_rate), 24+int(result['Y坐标值'])/stretch_rate).click().perform()time.sleep(1)except: continueclass_name = 'geetest_commit_tip'driver.find_element_by_class_name(class_name).click()break'''自动登陆成功!'''

自动登陆成功!!!

接下来贴完整版的全部代码:

import os.path
import random
import time
from selenium import *
from selenium import webdriver
from PIL import Image
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import base64
import json
import requests
from selenium.webdriver import ActionChainsurl='https://passport.bilibili.com/login'
driver = webdriver.Chrome(executable_path=r"C:\Users\ASUS\AppData\Local\Google\Chrome\Application\chromedriver.exe")
driver.set_window_size(1100, 958)
bool=Truewhile bool:driver.get(url)'''填写用户名和密码'''xpath='//*[@id="login-username"]'driver.find_element_by_xpath(xpath).send_keys('your_username')xpath='//*[@id="login-passwd"]'driver.find_element_by_xpath(xpath).send_keys('your_password')'''点击登录'''time.sleep(0.5)class_name='btn-login'driver.find_element_by_class_name(class_name).click()'''创建文件保存验证码'''image_name=str(int(1000000 * time.time()))+'.png'if not os.path.exists("yzm_small"):os.mkdir("yzm_small")if not os.path.exists("yzm_large"):os.mkdir("yzm_large")'''等待验证码图像出现'''class_name='geetest_tip_img'try:element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,class_name)))except Exception as e:print(e)continue'''截取验证码图片,并分为大图和小图验证码'''time.sleep(0.5)driver.save_screenshot('yzm.png')'''这里计算屏幕的拉伸率,不同电脑的windows缩放比例会影响到截图的像素点位置'''width=Image.open('yzm.png').size[0]stretch_rate=width/1100print('当前屏幕的缩放比例为:'+str(stretch_rate))img = Image.open('yzm.png')cropped = img.crop((800*stretch_rate,246*stretch_rate,925*stretch_rate,283*stretch_rate))  # (left, upper, right, lower)cropped.save('yzm_small/'+image_name)cropped = img.crop((668.5*stretch_rate,287*stretch_rate,925*stretch_rate,547*stretch_rate))  # (left, upper, right, lower)cropped.save('yzm_large/'+image_name)'''使用图灵验证码识别平台,进行验证码识别''''''图灵验证码识别平台:  http://www.tulingtech.xyz/static/index.html  '''def tuling_api(username, password, img_path, ID):with open(img_path, 'rb') as f:b64_data = base64.b64encode(f.read())b64 = b64_data.decode()data = {"username": username, "password": password, "ID": ID, "b64": b64}data_json = json.dumps(data)result = json.loads(requests.post("http://www.tulingtech.xyz/tuling/predict", data=data_json).text)return result'''小图部分识别'''img_path = 'yzm_small/'+image_nameresult_small = tuling_api(username="你的图灵验证码识别平台账号", password="你的图灵验证码识别平台密码", img_path=img_path, ID="02156188")result_small=result_small['result']print(result_small)'''大图部分识别'''img_path = 'yzm_large/' + image_nameresult_large = tuling_api(username="你的图灵验证码识别平台账号", password="你的图灵验证码识别平台密码", img_path=img_path, ID="05156485")print(result_large)class_name='geetest_tip_content'element = driver.find_element_by_class_name(class_name)try:for i in range(0, len(result_small)):result=result_large[result_small[i]]ActionChains(driver).move_to_element(element).move_by_offset(-70+int(result['X坐标值']/stretch_rate), 24+int(result['Y坐标值'])/stretch_rate).click().perform()time.sleep(1)except: continueclass_name = 'geetest_commit_tip'driver.find_element_by_class_name(class_name).click()break'''自动登陆成功!'''

有问题可以私信我吧,觉得写得不错麻烦给个三连好评哈哈~

使用Python+Selenium+图灵验证码识别平台,识别B站/bilibili的中文验证码,并自动登陆B站相关推荐

  1. 使用 图灵验证码识别平台+Python+Selenium,智能识别B站/bilibili的中文验证码,并实现自动登陆

    一直想用python写一个程序帮我自动登陆B站,完成一些点击任务,懂的都懂 =v= 最近终于腾出时间来搞了,其实最难的部分就是中文验证码的识别.这个借助API接口也能轻松搞定.下面分享一下全部源码(前 ...

  2. 微博模拟登陆的方法 + 图灵图像图片验证码识别平台 识别验证码(97%正确率)Python + Selenium+Chrome

    最近遇到一个问题,需要频繁切换账号登陆微博,但是需要识别微博的验证码,比较麻烦.而且因为需要24h不间断的操作,所以没法使用人工打码平台,而且打码平台也比较贵,延迟又高.最后找到了一个可以机器识别出来 ...

  3. 使用selenium自动登陆b站 图片文字验证识别

    文章目录 前言 一. 反,反反爬虫 1.反爬虫 2.反反爬虫 二,超级鹰 三.完整代码 1.导包 2.超级鹰接口 3.连接手动开启的浏览器 4.定位文本框标签 5.图片文字识别 6.文本处理,坐标处理 ...

  4. Python Selenium 抖音直播平台实现自动发送评论

    首先运行 test_01_save_cookies.py 手动登入个人的抖音账号 获得浏览器的 cookies, 并以pickle文件的格式保存到当前目录下. 然后打开文件 test_02_use_c ...

  5. Python Selenium UI 实现视频自动化播放

    Python Selenium UI 实现视频自动化播放 1.需求:实现某一课程自动播放 from selenium import webdriver from selenium.webdriver. ...

  6. Python 识别携程中文验证码(95%正确率)并自动登陆携程+图灵图像验证码识别平台

    这两天有一个业务需求,需要登陆不同的携程账号获取订单信息,但是由于携程有验证码检测机制,而且是个中文验证码比较难识别,试了几家人工打码平台,要么贵,要么延时高,要么没办法24小时运行.最后总算让我找到 ...

  7. Python使用selenium和百度AI开放平台识别验证码自动登录

    文章目录 本文内容 工具准备 Python Packages 浏览器驱动 准备登录 第一步   用Chrome浏览器打开目标网站 第二步   调用selenium输入账户密码 第三步   获取图片验证 ...

  8. python(自动化)利用selenium+百度ocr文字识别验证码实现自动登陆登陆CET-四级报名系统

    操作步骤: 1:登陆打开CET-考试系统 2:填写相关登陆信息 3:调用百度ocr实现文字验证码识别 4:实现登陆 如何使用和调用百度ocr文字识别接口 1:进入百度AI开发平台:链接 2:在页面上选 ...

  9. 验证码识别的原理python_蓝奏云数值验证码识别,python调用虹鱼图灵识别插件,超高正确率...

    本帖最后由 打字的小强 于 2020-6-5 13:11 编辑 识别验证码一直是本人想要做的事情,一直在接触按键精灵,了解到有一个虹鱼图灵识别插件专门做验证码和图像识别,原理就是图片处理和制作字库识别 ...

最新文章

  1. javascript中错误使用var造成undefined
  2. 抛开flash,自己开发实现C++ RTMP直播流播放器
  3. python max函数_Python max内置函数详细介绍
  4. win10笔记本永久删除文件文件怎样恢复
  5. 数学笔记3——导数3(隐函数的导数)
  6. 公布一个硬盘杀手的分析报告
  7. 特斯拉明年会有100万辆自动驾驶出租车 你敢坐吗?
  8. 调整样式_“寒来暑往”美国海军陆战队应季节调整迷彩样式的规定变化
  9. 移动广告聚合平台KeyMob:整合多家移动广告平台
  10. python程序设计方法学_python学习笔记(12)--程序设计方法学
  11. Vijos P1974 金币【数列】
  12. 日期格式 java_Java时间日期格式转换
  13. 一次JDBC与MySQL因“CST”时区协商误解导致时间差了13或14个小时
  14. linux之etc/fstab配置文件
  15. 《Boost》Part1 Minimum Spanning Tree
  16. java启动项目出现The Tomcat connector configured to listen on port 7014 failed to start. The port may alrea
  17. “随意软件”是什么?
  18. 天空卫士受邀成为四川省大数据发展研究会会长单位
  19. python怎么测试函数_快学Python:如何测试函数与类
  20. String字符串GBK转UTF8

热门文章

  1. 海鸥表表带太长了怎么拆_表带怎么拆,太长了想截掉两节?
  2. windows11安装SQL server数据库报错等待数据库引擎恢复句柄失败
  3. mysql数据库引擎常用面试总结
  4. 点云聚类算法总结---点云小白论文艰辛之路2
  5. 支付宝杀疯了,又开始「撒钱」啦
  6. 【JS继承】JS继承之组合继承
  7. 【贪心算法】舞蹈室的安排
  8. android argb白色,android颜色查询转换器(ARGB hex color converter)
  9. python 实时翻译_python3调用百度翻译API实现实时翻译
  10. 参数_kgl_large_heap_warning_threshold