Skip to content

使用PhantomJS浏览器加载不出验证码图片 #2

Description

@ijustlearn

因为本机运行环境问题,58版本的谷歌浏览器调用总是报错,我就改成使用PhantomJS浏览器验证,但是代码在click登陆按钮候加载不出来验证码图片

@LiuXingMing
代码如下:
`# encoding=utf-8

----------------------------------------

语言:Python2.7 #改成了3.6的运行代码

日期:2017-05-01

作者:九茶http://blog.csdn.net/bone_ace

功能:破解四宫格图形验证码,登录m.weibo.cn

----------------------------------------

import time
import random
from PIL import Image
from math import sqrt
from scrapyPro1.scrapyPro1.ims import ims
from io import StringIO,BytesIO
from selenium import webdriver
from selenium.webdriver.remote.command import Command
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
PIXELS = []

def getExactly(im):
""" 精确剪切"""
imin = -1
imax = -1
jmin = -1
jmax = -1
row = im.size[0]
col = im.size[1]
for i in range(row):
for j in range(col):
if im.load()[i, j] != 255:
imax = i
break
if imax == -1:
imin = i

for j in range(col):
    for i in range(row):
        if im.load()[i, j] != 255:
            jmax = j
            break
    if jmax == -1:
        jmin = j
return (imin + 1, jmin + 1, imax + 1, jmax + 1)

def getType(browser):
""" 识别图形路径 """
ttype = ''
time.sleep(3.5)
with open('test.png','wb') as f :
f.write(browser.get_screenshot_as_png())
im0 = Image.open(BytesIO(browser.get_screenshot_as_png()))
box = browser.find_element_by_id('patternCaptchaHolder')
im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L')
newBox = getExactly(im)
im = im.crop(newBox)
width = im.size[0]
height = im.size[1]
for png in ims.keys():
isGoingOn = True
for i in range(width):
for j in range(height):
if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # 以245为临界值,大约245为空白,小于245为线条;两个像素之间的差大约10,是为了去除245边界上的误差
isGoingOn = False
break
if isGoingOn is False:
ttype = ''
break
else:
ttype = png
else:
break
px0_x = box.location['x'] + 40 + newBox[0]
px1_y = box.location['y'] + 130 + newBox[1]
PIXELS.append((px0_x, px1_y))
PIXELS.append((px0_x + 100, px1_y))
PIXELS.append((px0_x, px1_y + 100))
PIXELS.append((px0_x + 100, px1_y + 100))
return ttype

def move(browser, coordinate, coordinate0):
""" 从坐标coordinate0,移动到坐标coordinate """
time.sleep(0.05)
length = sqrt((coordinate[0] - coordinate0[0]) ** 2 + (coordinate[1] - coordinate0[1]) ** 2) # 两点直线距离
if length < 4: # 如果两点之间距离小于4px,直接划过去
ActionChains(browser).move_by_offset(coordinate[0] - coordinate0[0], coordinate[1] - coordinate0[1]).perform()
return
else: # 递归,不断向着终点滑动
step = random.randint(3, 5)
x = int(step * (coordinate[0] - coordinate0[0]) / length) # 按比例
y = int(step * (coordinate[1] - coordinate0[1]) / length)
ActionChains(browser).move_by_offset(x, y).perform()
move(browser, coordinate, (coordinate0[0] + x, coordinate0[1] + y))

def draw(browser, ttype):
""" 滑动 """
if len(ttype) == 4:
px0 = PIXELS[int(ttype[0]) - 1]
login = browser.find_element_by_id('loginAction')
ActionChains(browser).move_to_element(login).move_by_offset(px0[0] - login.location['x'] - int(login.size['width'] / 2), px0[1] - login.location['y'] - int(login.size['height'] / 2)).perform()
browser.execute(Command.MOUSE_DOWN, {})

    px1 = PIXELS[int(ttype[1]) - 1]
    move(browser, (px1[0], px1[1]), px0)

    px2 = PIXELS[int(ttype[2]) - 1]
    move(browser, (px2[0], px2[1]), px1)

    px3 = PIXELS[int(ttype[3]) - 1]
    move(browser, (px3[0], px3[1]), px2)
    browser.execute(Command.MOUSE_UP, {})
else:
    print ('Sorry! Failed! Maybe you need to update the code.')

if name == 'main':
dcap = dict(DesiredCapabilities.PHANTOMJS) # PhantomJS需要使用老版手机的user-agent,不然验证码会无法通过
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
)
browser = webdriver.PhantomJS(desired_capabilities=dcap)
# options = webdriver.ChromeOptions()
# options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
# browser = webdriver.Chrome(chrome_options=options)
# browser = webdriver.Firefox()
browser.set_window_size(height=1050, width=840)
#browser.get('http://mail.haidilao.com')
browser.get('https://passport.weibo.cn/signin/login?entry=mweibo&r=http://weibo.cn/')

time.sleep(5)
name = browser.find_element_by_id('loginName')
psw = browser.find_element_by_id('loginPassword')
login = browser.find_element_by_id('loginAction')
name.send_keys('测试账号')  # 测试账号
psw.send_keys('测试账号密码')
login.click()

ttype = getType(browser)  # 识别图形路径
print ('Result: %s!' % ttype)
draw(browser, ttype)  # 滑动破解
time.sleep(20)
browser.close()

报错如下: C:\Users\0\AppData\Local\Programs\Python\Python36\python.exe G:/Myproject/scrapyPro1/scrapyPro1/login.py Traceback (most recent call last): File "G:/Myproject/scrapyPro1/scrapyPro1/login.py", line 142, in <module> ttype = getType(browser) # 识别图形路径 File "G:/Myproject/scrapyPro1/scrapyPro1/login.py", line 55, in getType box = browser.find_element_by_id('patternCaptchaHolder') File "C:\Users\0\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 285, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Users\0\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element 'value': value})['value'] File "C:\Users\0\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute self.error_handler.check_response(response) File "C:\Users\0\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Error Message => 'Unable to find element with id 'patternCaptchaHolder'' caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"101","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62340","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"id\", \"value\": \"patternCaptchaHolder\", \"sessionId\": \"b17de770-387f-11e7-8a98-152c54fe765a\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/b17de770-387f-11e7-8a98-152c54fe765a/element"} Screenshot: available via screen
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions