selenium登录 京东滑动验证码
2018-12-09 11:21:56来源:博客园 阅读 ()
京东的滑动验证码在页面上是没有原图的,所有我是用ps把他们拼成一个的。
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from PIL import Image import time import random def if_is_s(img_1, img_2):#判断是不是跟比较的是一张图 a = 0 for i in range(1, img_1.size[1]): for j in range(1, 3): rgb1 = img_1.load()[i, j] rgb2 = img_2.load()[i, j] res1 = abs(rgb1[0] - rgb2[0]) res2 = abs(rgb1[1] - rgb2[1]) res3 = abs(rgb1[2] - rgb2[2]) if res1 < 10 and res2 < 10 and res3 < 10: a += 1 else: a = 0 if a > 50: return True return False def get_distance(image1,image2):#找出需要滑动的距离 a = 0 threshold=60 left=57 for i in range(left,image1.size[0]): for j in range(image1.size[1]): rgb1=image1.load()[i,j] rgb2=image2.load()[i,j] res1=abs(rgb1[0]-rgb2[0]) res2=abs(rgb1[1]-rgb2[1]) res3=abs(rgb1[2]-rgb2[2]) if not (res1 < threshold and res2 < threshold and res3 < threshold): a += 1 return i+20 return i+20 def get_tracks(distance):#这是滑动轨迹的,京东的滑动轨迹很恶心,这个也是有时可以有时不行 tracks = [] current = 0 mid = distance * 4 / 5 t = 0.2 v = 0 while current < distance: if current < mid: a = random.uniform(2, 5) else: a = -(random.uniform(12.5, 13.5)) v0 = v v = v0 + a * t x = v0 * t + 1 / 2 * a * (t ** 2) current += x if 0.6 < current - distance < 1: x = x - 0.53 tracks.append(round(x, 2)) elif 1 < current - distance < 1.5: x = x - 1.4 tracks.append(round(x, 2)) elif 1.5 < current - distance < 3: x = x - 1.8 tracks.append(round(x, 2)) else: tracks.append(round(x, 2)) if sum(tracks) > distance: i = sum(tracks) - distance tracks[-1] = round(tracks[-1] - i, 2) i = random.uniform(1, 3 ) i = round(i, 2) zz = random.uniform(0.1, 0.3) tracks[-1] = round(tracks[-1] + i, 2) i = round(i - zz, 2) tracks.append(-i) if sum(tracks) < distance: si = distance - sum(tracks) tracks.append(round(si, 2)) print(tracks, sum(tracks)) return tracks if __name__=='__main__': browser = webdriver.Firefox() browser.get('https://passport.jd.com/new/login') butt = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CLASS_NAME,'link-login'))) butt.click() butt = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH,'//a[@clstag="pageclick|keycount|login_pc_201804112|10"]'))) butt.click() user = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.ID,'loginname'))) password = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.ID,'nloginpwd'))) user.send_keys('xxx') time.sleep(2) password.send_keys('xxx') izsz = 1 while izsz == 1:#无限循环直到登录成功 try: button = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CLASS_NAME,'btn-img.btn-entry'))) time.sleep(2) button.click() time.sleep(2) browser.save_screenshot('1.png')#截图 page_snap_obj = Image.open('1.png') img = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CLASS_NAME,'JDJRV-bigimg'))) local = img.location size = img.size top = local['y'] bottom = local['y'] + size['height'] left = local['x'] right = local['x'] + size['width'] img_1 = Image.open('index2.png') img_2 = page_snap_obj.crop((left, top, right, bottom))#裁剪图片尺寸 img_2.save('2.png')#保存 i = if_is_s(img_1,img_2)#判断是不是一张图,不是点击换一张 while not i: time.sleep(2) butt = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.CLASS_NAME,'JDJRV-img-refresh'))) butt.click() time.sleep(2) browser.save_screenshot('1.png') page_snap_obj = Image.open('1.png') img = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'JDJRV-bigimg'))) local = img.location size = img.size top = local['y'] bottom = local['y'] + size['height'] left = local['x'] right = local['x'] + size['width'] img_1 = Image.open('index2.png') img_2 = page_snap_obj.crop((left, top, right, bottom)) img_2.save('2.png') time.sleep(2) i = if_is_s(img_1, img_2) ji = get_distance(img_1, img_2) print(ji) tracks = get_tracks(ji) ijs = len(tracks)//5*3 tracks1 = tracks[:ijs]#对滑动轨迹继续改变 tracks2 = tracks[ijs:] z = sum(tracks1) tracks2.insert(0,z) tracks2.insert(0,0) button = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="JDJRV-slide-inner JDJRV-slide-btn"]'))) ActionChains(browser).click_and_hold(button).perform() for track in tracks: ActionChains(browser).move_by_offset(xoffset=track, yoffset=0).perform() time.sleep(0.5) # 0.5秒后释放鼠标 ActionChains(browser).release().perform() url = browser.current_url if 'https://passport.jd.com/new/login.aspx?' in str(url): izsz = 1 else: izsz = 0 except: izsz = 1
难点就在滑动的轨迹太容易被抓,而且一种用多了也会被识别。
git https://github.com/18370652038/jd.git
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Django项目中使用qq第三方登录。 2019-08-13
- python day2-爬虫实现github登录 2019-08-13
- python爬虫学习之淘宝模拟登录 2019-08-13
- 爬虫--selenium之 chromedriver与chrome版本映射表(最新至v 2019-07-24
- python+selenium实现自动化百度搜索关键词 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash