python 关于某网站的模拟登陆,破解滑块登陆!!!!
2019-06-30 10:53
316 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/markme/article/details/94294724
利用python 实现模拟登陆网站,并破解滑块登陆
缘由
起因是一次需求。我想对某个视频网站模拟登陆!!!
本文使用senenium +python+相应的模块
可能你需要安装cv2,可以直接调用下面语句:
pip install opencv-python
模拟登陆的是头条:
代码如下
from selenium.webdriver import ActionChains from selenium import webdriver from selenium.webdriver.common.by import By import time, requests import cv2 from PIL import Image,ImageEnhance from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from io import BytesIO import numpy as np #首先定义一个破解滑块的类,这个是重点请仔细看 class Crackslide( ): """ 通过浏览器截图,识别验证码中缺口位置,获取需要滑动距离,并模仿人类行为破解滑动验证码 """ def __init__(self): super(Crackslide,self).__init__() self.url = 'https://sso.toutiao.com/login/?service=https://mp.toutiao.com/sso_confirm/?redirect_url=JTJGcHJvZmlsZV92MyUyRnhpZ3VhJTJGdXBsb2FkLXZpZGVv'#这就是你想登陆的网站,要自己输入 self.driver = webdriver.Chrome self.wait = WebDriverWait(self.driver,20) self.zoom=2 def open(self): self.driver.get(self.url) #定义一个获得目标图片和滑动模块的图片的获得 def get_pic(self): time.sleep(2) target = browser.find_element_by_id("validate-big") template = browser.find_element_by_class_name("validate-block") target_link = target.get_attribute('src') template_link = template.get_attribute('src') target_img = Image.open(BytesIO(requests.get(target_link).content)) template_img = Image.open(BytesIO(requests.get(template_link).content)) target_img.save('target.jpg') template_img.save('template.png') size_orign = target.size local_img = Image.open('target.jpg') size_loc = local_img.size self.zoom=320 / int(size_loc[0]) #定义等下行走的路径,用于模拟人类拖动滑块的行为 def get_tracks(self,distance): print(distance) distance+=20 v=0 t=0.2 forward_tracks = [] current = 0 mid = distance *3/5 while current < distance: if current<mid: a = 2 else: a = -3 s = v * t +0.5 * a *(t**2) v = v + a * t current+=s forward_tracks.append(round(s)) back_track = [-3,-3,-2,-2,-2,-2,-2,-1,-1,-1] return {'forward_tracks':forward_tracks,'back_tracks':back_track} #把图片转化成矩阵,为上一个模拟路径提供相应的参数 def match(self, target, template): img_rgb = cv2.imread(target) img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread(template, 0) run = 1 w, h = template.shape[::-1] print(w, h) res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) # 使用二分法查找阈值的精确值 L = 0 R = 1 while run < 20: run += 1 threshold = (R + L) / 2 if threshold < 0: print('Error') return None loc = np.where(res >= threshold) # print(len(loc[1])) if len(loc[1]) > 1: L += (R - L) / 2 elif len(loc[1]) == 1: print('目标区域起点x坐标为:%d' % loc[1][0]) break elif len(loc[1]) < 1: R -= (R - L) / 2 return loc[1][0] #开始模拟匹配 def crack_slider(self, browser): # self.open() target = 'target.jpg' template = 'template.png' self.get_pic() distance = self.match(target, template) zoo = 1 # 缩放系数,需要自己调整大小 tracks = self.get_tracks((distance + 4) * zoo) # 对位移的缩放计算 # print(tracks) slider = browser.find_element_by_class_name("drag-button") ActionChains(browser).click_and_hold(slider).perform() for track in tracks['forward_tracks']: ActionChains(browser).move_by_offset(xoffset=track, yoffset=0).perform() time.sleep(0.5) for back_tracks in tracks['back_tracks']: ActionChains(browser).move_by_offset(xoffset=back_tracks, yoffset=0).perform() ActionChains(browser).move_by_offset(xoffset=-3, yoffset=0).perform() ActionChains(browser).move_by_offset(xoffset=3, yoffset=0).perform() time.sleep(0.5) ActionChains(browser).release().perform() try: failure = WebDriverWait(browser, 5).until( EC.text_to_be_present_in_element((By.ID, 'validate-prompt'), '按住左边按钮拖动完成上方拼图')) print(failure) except: print('验证成功') return None if failure: self.crack_slider(browser) browser = webdriver.Chrome() #下面的url以及相应的id,class要从你选区的网站你自己去找!! url='https://sso.toutiao.com/login/?service=https://mp.toutiao.com/sso_confirm/?redirect_url=JTJGcHJvZmlsZV92MyUyRnhpZ3VhJTJGdXBsb2FkLXZpZGVv'#你想要登陆的网址 browser.get(url) browser.find_element_by_class_name('login-type-icon').click() browser.find_element_by_id("user-name").send_keys("你的账号") time.sleep(1) browser.find_element_by_id("password").send_keys("你的密码") browser.find_element_by_id("bytedance-login-submit").click() c=Crackslide() c.crack_slider(browser)
重点
要注意关于上方zoo的调解,才能到达想要的匹配结果
相关文章推荐
- 详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- python模拟登陆网站
- python模拟登陆网站
- 黄聪:关于抓取网页,分析网页内容,模拟登陆网站的逻辑/流程和注意事项(转)
- 【python学习】模拟登陆网站
- Python3.5.2模拟网站登陆
- python模拟浏览器webdriver登陆网站后抓取页面并输出
- python爬虫学习之路(1)_ CSDN网站的模拟登陆
- 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站
- 黄聪:关于抓取网页,分析网页内容,模拟登陆网站的逻辑/流程和注意事项(转)
- python模拟网站登陆
- Python模拟用户登陆网站
- Python使用requests库模拟登陆网站的方式--以豆瓣为例
- Python使用requests库模拟登陆网站的方式--以豆瓣为例
- python爬虫实战-模拟登陆网站(验证码手工输入)
- (转)Python-详解抓取网站,模拟登陆,抓取动态网页的原理和实现
- 详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- Python模拟浏览器登陆网站
- python模拟浏览器登陆人人网站
- python之selenium模拟网站登陆