欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 《破解验证码:用Requests和Selenium实现模拟登录的终极指南》

《破解验证码:用Requests和Selenium实现模拟登录的终极指南》

2024/10/25 9:33:55 来源:https://blog.csdn.net/m0_74087660/article/details/140733665  浏览:    关键词:《破解验证码:用Requests和Selenium实现模拟登录的终极指南》

两种模拟登录方式(图形验证码)

超级鹰

打码平台,用于识别验证码

requests模拟登录

from chaojiying import Chaojiying_Client
import requests
from requests import Session
from lxml import etree
#获取图片信息
def get_pic_info(img_name):chaojiying = Chaojiying_Client()im = open(img_name, 'rb').read()if chaojiying.PostPic(im, 1902)['err_no']==0:return chaojiying.PostPic(im, 1902)['pic_str']s = Session()
#请求时已生成cookie并且存在s中
img = s.get('http://www.chaojiying.com//include/code/code.php?u=1').content#拿到一个二进制数据
op = open('a.jpg','wb')#二进制形式写入
op.write(img)
op.close()
yan = get_pic_info('a.jpg')
#登录
data = {'user': '17526625714','pass':'31415926AsD@','imgtxt': yan,'act': 1}
source = s.post('http://www.chaojiying.com/user/login/',data=data).text
print(source)#返回源码
score = etree.HTML(source).xpath('//span[@class="cred strong num"]/text()')
print(score)#返回账户题分

selenium模拟登录

进行抠图时要进行定位,定位前要将电脑的显示设置设为100%

from chaojiying import Chaojiying_Client
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
#用来处理图片的包
from PIL import Image
import time
def get_pic_info(img_name):chaojiying = Chaojiying_Client()im = open(img_name, 'rb').read()if chaojiying.PostPic(im, 1902)['err_no']==0:return chaojiying.PostPic(im, 1902)['pic_str']
options = webdriver.ChromeOptions()
# 设置无头模式
# options.add_argument('--headless')# selenium新版本: 将谷歌驱动网址用service包裹一下
service = Service('D:\extention\spider\day4\chormedriver\chromedriver-win64\chromedriver.exe')
# 驱动
dr = webdriver.Chrome(service=service, options=options)
dr.get('https://www.chaojiying.com/user/login/')
# 浏览器最大化
dr.maximize_window()
time.sleep(2)
# 截图浏览器保存到bdbutton.png图片
dr.save_screenshot('bdbutton.png')
# 清除用户名输入框里的东西
dr.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').clear()
# 填用户名
dr.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys('yizhiqie')
# 清除密码输入框里的东西
dr.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').clear()
# 填密码
dr.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys('18532104295')
# 定位验证码图片对象
element = dr.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img')
# 获取图片的左边距上边距右边距下边距
left = element.location['x']
top = element.location['y']
right = left + element.size['width']
bottom = top + element.size['height']
# 根据边距定位验证码图片,抠出验证码图片并保存为button.png
im = Image.open('bdbutton.png')
im = im.crop((left, top, right, bottom))
im.save('button.png')yan = dr.find_element(By.XPATH,'//input[@name="imgtxt"]')
yan.send_keys(get_pic_info('button.png'))
dr.find_element(By.XPATH,'//input[@class="login_form_input_submit"]').click()
time.sleep(10)
score = dr.find_element(By.XPATH,'//span[@class="cred strong num"]').text
print(score)

更多精致内容:[CodeRealm]

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com