Python使用selenium实现网页用户名 密码 验证码自动登录功能
作者:clarkxhb 时间:2023-11-14 18:58:45
好久没有学python了,反正各种理由吧(懒惰总会有千千万万的理由),最近网上学习了一下selenium,实现了一个简单的自动登录网页,具体如下。
1.安装selenium:
如果你已经安装好anaconda3,直接在windows的dos窗口输入命令安装selenium:
python -m pip install --upgrade pip
查看版本pip show selenium
2.接着去http://chromedriver.storage.googleapis.com/index.html下载chromedriver.exe(根据chrome的版本下载对应的)
3.将下载好的chromedriver.exe解压后放到指定目录
4.安装tesseract-ocr.exe 配置环境变量
5.安装pytesseract : pip install pytesseract
6.python脚本
思路:6.1登录页面按F12检查元素,获取用户名 密码 验证码 验证码图片的元素id
6.2.调用chromedriver
6.3.截取验证码图片的位置
6.4.pytesseract识别图片中字符,最后验证码识别为空!!???这个待解决
6.5.脚本如下:
from selenium import webdriver
from PIL import Image
import pytesseract
import os,time
chromedriver = "D:\Program Files\Anaconda3\selenium\webdriver\chromedriver.exe" #这里写本地的chromedriver 的所在路径
os.environ["webdriver.Chrome.driver"] = chromedriver #调用chrome浏览器
driver = webdriver.Chrome(chromedriver)
driver.get("http://xxxx.com") #该处为具体网址
driver.refresh() #刷新页面
driver.maximize_window() #浏览器最大化
#获取全屏图片,并截取验证码图片的位置
driver.get_screenshot_as_file('a.png')
location = driver.find_element_by_id('imgValidateCode').location
size = driver.find_element_by_id('imgValidateCode').size
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
a = Image.open("a.png")
im = a.crop((left,top,right,bottom))
im.save('a.png')
time.sleep(1)
#打开保存的验证码图片
image = Image.open("a.png")
#图片转换成字符
vcode = pytesseract.image_to_string(image)
print(vcode)
#填充用户名 密码 验证码
driver.find_element_by_id("staffCode").send_keys("username")
driver.find_element_by_id("pwd").send_keys("password")
driver.find_element_by_id("validateCode").send_keys(vcode)
#点击登录
driver.find_element_by_id("loginBtn").click()
总结
以上所述是小编给大家介绍的Python实现网页用户名 密码 验证码自动登录功能网站的支持!
来源:http://www.cnblogs.com/clarkxhb/p/autologinweb.html
标签:python,自动登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
sql server中Select count(*)和Count(1)的区别和执行方式
2024-01-23 11:47:39
![](https://img.aspxhome.com/file/2023/8/110938_0s.png)
golang中使用匿名结构体的方法
2023-07-10 07:26:56
vue如何截取字符串
2024-04-30 10:21:15
![](https://img.aspxhome.com/file/2023/2/130262_0s.png)
asp如何将统计的访问者数目周期性地保存?
2009-11-26 20:54:00
mysql的日期和时间函数
2024-01-22 12:46:18
Python自动录入ERP系统数据
2022-03-09 06:05:41
![](https://img.aspxhome.com/file/2023/2/127912_0s.png)
js实现用div层模拟的小窗口
2007-12-02 15:01:00
![](https://img.aspxhome.com/file/UploadPic/200712/2/2007122151314277s.gif)
SQL Server 2016 CTP2.2安装配置方法图文教程
2024-01-18 19:04:29
![](https://img.aspxhome.com/file/2023/5/92735_0s.png)
python实现超市商品销售管理系统
2021-01-06 22:18:14
![](https://img.aspxhome.com/file/2023/9/93529_0s.jpg)
为什么GO不支持循环引用
2024-02-08 15:10:01
![](https://img.aspxhome.com/file/2023/9/113099_0s.png)
Python的 元组(Tuple)详解
2021-09-07 13:55:18
![](https://img.aspxhome.com/file/2023/4/120334_0s.png)
Go语言协程处理数据有哪些问题
2024-02-12 04:54:53
python在windows调用svn-pysvn的实现
2022-03-15 05:13:48
PHP伪静态页面函数附使用方法
2023-11-22 06:25:42
JavaScript原生对象常用方法总结(推荐)
2024-05-05 09:15:20
Python使用Pandas库常见操作详解
2022-12-10 09:03:13
Golang交叉编译(跨平台编译)的使用
2024-05-02 16:26:40
![](https://img.aspxhome.com/file/2023/6/130706_0s.png)
数据库分页大全(mssql,mysql,oracle)
2010-10-25 20:02:00
Linux PHP连接MSSQL
2011-06-02 12:01:00
Python PyQt拖动控件对齐到网格的方法步骤
2022-05-04 19:25:00
![](https://img.aspxhome.com/file/2023/6/97316_0s.png)