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

178 阅读2分钟

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

超级鹰

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

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]

公众号.png