1. 程式人生 > >selenium破解人人登陸驗證碼

selenium破解人人登陸驗證碼

from selenium import webdriver
from PIL import Image
from chaojiying import Chaojiying_Client
import time

driver = webdriver.Chrome()

driver.get('http://ww.renren.com')

driver.find_element_by_xpath('//*[@id="email"]').send_keys("人人網賬號")
driver.find_element_by_xpath('//*[@id="password"]').send_keys('password')
#截圖
driver.save_screenshot("renren.png")
#獲取寬高和座標(查詢驗證碼所在元素,獲取驗證碼的座標和寬高,)
img = driver.find_element_by_xpath('//*[@id="verifyPic_login"]')
#獲取x座標
x = img.location['x']
#y座標
y = img.location['y']
#獲取寬
width = img.size['width']
#獲取高
height = img.size['height']
#摳圖(在renren.png中摳圖) pip install PIL
screen = Image.open('renren.png') #載入截圖

code = screen.crop((x,y,x+width,y+height))#截出驗證碼
code.save('code.png') #將驗證碼儲存
time.sleep(2)
#傳送給超級鷹破解
chaojiying = Chaojiying_Client('超級鷹賬號', 'password', '96001')
im = open('code.png', 'rb').read()
#2004 是超級鷹破解驗證碼的驗證碼型別
c = chaojiying.PostPic(im,2004)['pic_str']
print(c)
#將驗證碼輸入到框裡
driver.find_element_by_xpath('//*[@id="icode"]').send_keys(c)
time.sleep(3)
#點選登陸
driver.find_element_by_xpath('//*[@id="login"]').click()