自動登陸博客園以後臺驗證碼

驗證碼

#破解博客園後臺登陸
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from PIL import Image import time def get_snap(): driver.save_screenshot('full_snap.png') page_snap_obj=Image.open('full_snap.png') return page_snap_obj def get_image(): img=driver.find_element_by_class_name('geetest_canvas_img') time.sleep(2) location=img.location size=img.size left=location['x'] top=location['y'] right=left+size['width'] bottom=top+size['height'] page_snap_obj=get_snap() image_obj=page_snap_obj.crop((left,top,right,bottom)) # image_obj.show()
    return image_obj def get_distance(image1,image2): start=57 threhold=60

    for i in range(start,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]) # print(res1,res2,res3)
            if not (res1 < threhold and res2 < threhold and res3 < threhold): return i-7
    return i-7

def get_tracks(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_tracks=[-3,-3,-2,-2,-2,-2,-2,-1,-1,-1] #總共等於-20

    return {'forward_tracks':forward_tracks,'back_tracks':back_tracks} try: # 一、輸入帳號密碼回車
    driver = webdriver.Chrome() driver.implicitly_wait(3) driver.get('https://passport.cnblogs.com/user/signin') username = driver.find_element_by_id('input1') pwd = driver.find_element_by_id('input2') signin = driver.find_element_by_id('signin') username.send_keys('******')  #用戶名
    pwd.send_keys('******')    #密碼
 signin.click() # 二、點擊按鈕,獲得沒有缺口的圖片
    button = driver.find_element_by_class_name('geetest_radar_tip') button.click() # 三、獲取沒有缺口的圖片
    image1 = get_image() # 四、點擊滑動按鈕,獲得有缺口的圖片
    button = driver.find_element_by_class_name('geetest_slider_button') button.click() # 五、獲取有缺口的圖片
    image2 = get_image() # 六、對比兩種圖片的像素點,找出位移
    distance = get_distance(image1, image2) # 七、模擬人的行爲習慣,根據總位移獲得行爲軌跡
    tracks = get_tracks(distance) print(tracks) # 八、按照行動軌跡先正向滑動,後反滑動
    button = driver.find_element_by_class_name('geetest_slider_button') ActionChains(driver).click_and_hold(button).perform() # 正常人類老是自信滿滿地開始正向滑動,自信地表現是瘋狂加速
    for track in tracks['forward_tracks']: ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 結果傻逼了,正常的人類停頓了一下,回過神來發現,臥槽,滑過了,而後開始反向滑動
    time.sleep(0.5) for back_track in tracks['back_tracks']: ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform() # 小範圍震盪一下,進一步迷惑極驗後臺,這一步能夠極大地提升成功率
    ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform() # 成功後,騷包人類總喜歡默默地欣賞一下本身拼圖的成果,而後依依不捨地鬆開那隻髒手
    time.sleep(0.5) ActionChains(driver).release().perform() time.sleep(10)  # 睡時間長一點,肯定登陸成功
finally: driver.close()





#總結:測試了幾下,感受就是驗證碼的位置是隨機變化的,可是此碼給出的信息倒是不變的,so,pass

再來:參考

###############優化後的代碼(將功能封裝成函數調用)#######
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By #按照什麼方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #鍵盤按鍵操做
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait #等待頁面加載某些元素
from PIL import Image #pip3 install pillow

import time def get_snap(driver): driver.save_screenshot('snap.png')#截圖
    snap_obj=Image.open('snap.png')#保存
    return snap_obj def get_image(driver): img=driver.find_element_by_class_name('geetest_canvas_img') time.sleep(2) #等待圖片加載完畢
    size=img.size location=img.location #獲取圖片位置
    left=location['x'] top=location['y'] right=left+size['width'] bottom=top+size['height'] snap_obj=get_snap(driver) image_obj=snap_obj.crop((left,top,right,bottom))#截圖操做
    # image_obj.show()
    return image_obj def get_distance(image1,image2): start_x=58#滑塊最左側
    threhold=60#去除僞影響
    # print(image1.size)
    # print(image2.size)
    for x in range(start_x,image1.size[0]): for y in range(image1.size[1]): rgb1=image1.load()[x,y] rgb2=image2.load()[x,y] res1=abs(rgb1[0]-rgb2[0]) res2=abs(rgb1[1]-rgb2[1]) res3=abs(rgb1[2]-rgb2[2]) if not (res1 < threhold and res2 < threhold and res3 < threhold): return x-7#偏差範圍

def get_tracks(distance): distance+=20#故意劃過頭20像素
    #v=v0+a*t
    #s=v*t+0.5*a*(t**2)
 v0=0 s=0 t=0.2 mid=distance*3/5 forward_tracks=[] while s < distance: if s < mid: a=2
        else: a=-3 v=v0 track=v*t+0.5*a*(t**2) track=round(track)#取整數
        v0=v+a*t s+=track forward_tracks.append(track) back_tracks=[-1,-1,-1,-2,-2,-2,-3,-3,-2,-2,-1] #20
    return {"forward_tracks":forward_tracks,'back_tracks':back_tracks} def crack(driver):#封裝滑動的函數
    # 二、點擊驗證人機按鈕,彈出沒有缺口的圖
    button = driver.find_element_by_class_name('geetest_radar_tip_content') button.click() # 三、針對沒有缺口的圖片進行截圖
    image1 = get_image(driver) # 四、點擊滑動按鈕,彈出有缺口的圖
    slider_button = driver.find_element_by_class_name('geetest_slider_button') slider_button.click() # 五、針對有缺口的圖片進行截圖
    image2 = get_image(driver) # 六、對比兩張圖片,找出缺口,即滑動的位移
    distance = get_distance(image1, image2) # print(distance)

    # 七、按照人的行爲行爲習慣,把總位移切成一段段小的位移
    traks_dic = get_tracks(distance) # 八、按照位移移動
    slider_button = driver.find_element_by_class_name('geetest_slider_button') ActionChains(driver).click_and_hold(slider_button).perform() # 按住不放手
    # 先向前移動
    forward_tracks = traks_dic["forward_tracks"] back_tracks = traks_dic["back_tracks"] for forward_track in forward_tracks: ActionChains(driver).move_by_offset(xoffset=forward_track, yoffset=0).perform() # 短暫停頓,發現傻逼,移過了
    time.sleep(0.2) # 先向後移動
    for back_track in back_tracks: ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform() # 抖一抖
    ActionChains(driver).move_by_offset(xoffset=-4, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=-2, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform() time.sleep(0.3) ActionChains(driver).release().perform() # 鬆開鼠標



def login_cnblogs(username,pwd): driver = webdriver.Chrome()  # 谷歌瀏覽器driver = webdriver.Chrome()#谷歌瀏覽器
    try: driver.get('https://passport.cnblogs.com/user/signin')#博客園
        driver.implicitly_wait(10)#隱形等待10秒

        #一、輸入帳號、密碼,而後點擊登錄
        input_user=driver.find_element_by_id('input1') input_pwd=driver.find_element_by_id('input2') login_button=driver.find_element_by_id('signin') input_user.send_keys(username)#輸入帳號
        input_pwd.send_keys(pwd)#輸入密碼
        login_button.click()#點擊登陸按鈕
        # 調用 封裝滑動的函數
 crack(driver) time.sleep(10) finally: driver.close() if __name__ == '__main__': login_cnblogs(username='腳本小孩',pwd='*****')

仍是出錯,緣由在第4個def
def get_tracks(distance): distance+=20#故意劃過頭20像素
」  待解惑

 

 

繼續啊

#首先要安裝Pillow pip3 install pillow #Pillow:基於PIL,處理python 3.x的圖形圖像庫.由於PIL只能處理到python 2.x,而這個模塊能處理Python3.x,目前用它作圖形的不少.

# 破解滑動驗證碼自動登陸博客園 ###########思路整理##########

from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By #按照什麼方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #鍵盤按鍵操做
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait #等待頁面加載某些元素
from PIL import Image #pip3 install pillow

import time def get_snap(driver): driver.save_screenshot('snap.png')#截圖
    snap_obj=Image.open('snap.png')#保存
    return snap_obj def get_image(driver): img=driver.find_element_by_class_name('geetest_canvas_img') time.sleep(2) #等待圖片加載完畢
    size=img.size location=img.location #獲取圖片位置
    left=location['x'] top=location['y'] right=left+size['width'] bottom=top+size['height'] snap_obj=get_snap(driver) image_obj=snap_obj.crop((left,top,right,bottom))#截圖操做
    # image_obj.show()
    return image_obj def get_distance(image1,image2): start_x=58#滑塊最左側
    threhold=60#去除僞影響
    # print(image1.size)
    # print(image2.size)
    for x in range(start_x,image1.size[0]): for y in range(image1.size[1]): rgb1=image1.load()[x,y] rgb2=image2.load()[x,y] res1=abs(rgb1[0]-rgb2[0]) res2=abs(rgb1[1]-rgb2[1]) res3=abs(rgb1[2]-rgb2[2]) if not (res1 < threhold and res2 < threhold and res3 < threhold): return x-7#偏差範圍

def get_tracks(distance): distance+=20#故意劃過頭20像素
    #v=v0+a*t
    #s=v*t+0.5*a*(t**2)
 v0=0 s=0 t=0.2 mid=distance*3/5 forward_tracks=[] while s < distance: if s < mid: a=2
        else: a=-3 v=v0 track=v*t+0.5*a*(t**2) track=round(track)#取整數
        v0=v+a*t s+=track forward_tracks.append(track) back_tracks=[-1,-1,-1,-2,-2,-2,-3,-3,-2,-2,-1] #20
    return {"forward_tracks":forward_tracks,'back_tracks':back_tracks} try: driver = webdriver.Chrome()#谷歌瀏覽器
    driver.get('https://passport.cnblogs.com/user/signin')#博客園
    driver.implicitly_wait(10)#隱形等待10秒

    #一、輸入帳號、密碼,而後點擊登錄
    input_user=driver.find_element_by_id('input1') input_pwd=driver.find_element_by_id('input2') login_button=driver.find_element_by_id('signin') input_user.send_keys('腳本小孩')#輸入帳號
    input_pwd.send_keys('********')#輸入密碼
    login_button.click()#點擊登陸按鈕

    #二、點擊驗證人機按鈕,彈出沒有缺口的圖
    button=driver.find_element_by_class_name('geetest_radar_tip_content') button.click() #三、針對沒有缺口的圖片進行截圖
    image1=get_image(driver) #四、點擊滑動按鈕,彈出有缺口的圖
    slider_button=driver.find_element_by_class_name('geetest_slider_button') slider_button.click() #五、針對有缺口的圖片進行截圖
    image2=get_image(driver) #六、對比兩張圖片,找出缺口,即滑動的位移
    distance=get_distance(image1,image2) # print(distance)

    #七、按照人的行爲行爲習慣,把總位移切成一段段小的位移
    traks_dic=get_tracks(distance) #八、按照位移移動
    slider_button=driver.find_element_by_class_name('geetest_slider_button') ActionChains(driver).click_and_hold(slider_button).perform()#按住不放手
    #先向前移動
    forward_tracks=traks_dic["forward_tracks"] back_tracks=traks_dic["back_tracks"] for forward_track in forward_tracks: ActionChains(driver).move_by_offset(xoffset=forward_track,yoffset=0).perform() #短暫停頓,發現傻逼,移過了
    time.sleep(0.2) # 先向後移動
    for back_track in back_tracks: ActionChains(driver).move_by_offset(xoffset=back_track,yoffset=0).perform() # 抖一抖
    ActionChains(driver).move_by_offset(xoffset=-4,yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=-2,yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform() time.sleep(0.3) ActionChains(driver).release().perform()#鬆開鼠標
 time.sleep(10) finally: driver.close()

 

補充:html

from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from PIL import Image import time def get_snap(driver): driver.save_screenshot('full_snap.png') page_snap_obj=Image.open('full_snap.png') return page_snap_obj def get_image(driver): img=driver.find_element_by_class_name('geetest_canvas_img') time.sleep(2) location=img.location size=img.size left=location['x'] top=location['y'] right=left+size['width'] bottom=top+size['height'] page_snap_obj=get_snap(driver) image_obj=page_snap_obj.crop((left,top,right,bottom)) # image_obj.show()
    return image_obj def get_distance(image1,image2): start=57 threhold=60

    for i in range(start,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]) # print(res1,res2,res3)
            if not (res1 < threhold and res2 < threhold and res3 < threhold): return i-7
    return i-7

def get_tracks(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_tracks=[-3,-3,-2,-2,-2,-2,-2,-1,-1,-1] #總共等於-20

    return {'forward_tracks':forward_tracks,'back_tracks':back_tracks} def crack(driver): #破解滑動認證
    # 一、點擊按鈕,獲得沒有缺口的圖片
    button = driver.find_element_by_class_name('geetest_radar_tip') button.click() # 二、獲取沒有缺口的圖片
    image1 = get_image(driver) # 三、點擊滑動按鈕,獲得有缺口的圖片
    button = driver.find_element_by_class_name('geetest_slider_button') button.click() # 四、獲取有缺口的圖片
    image2 = get_image(driver) # 五、對比兩種圖片的像素點,找出位移
    distance = get_distance(image1, image2) # 六、模擬人的行爲習慣,根據總位移獲得行爲軌跡
    tracks = get_tracks(distance) print(tracks) # 七、按照行動軌跡先正向滑動,後反滑動
    button = driver.find_element_by_class_name('geetest_slider_button') ActionChains(driver).click_and_hold(button).perform() # 正常人類老是自信滿滿地開始正向滑動,自信地表現是瘋狂加速
    for track in tracks['forward_tracks']: ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 結果傻逼了,正常的人類停頓了一下,回過神來發現,臥槽,滑過了,而後開始反向滑動
    time.sleep(0.5) for back_track in tracks['back_tracks']: ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform() # 小範圍震盪一下,進一步迷惑極驗後臺,這一步能夠極大地提升成功率
    ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform() ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform() # 成功後,騷包人類總喜歡默默地欣賞一下本身拼圖的成果,而後依依不捨地鬆開那隻髒手
    time.sleep(0.5) ActionChains(driver).release().perform() def login_cnblogs(username,password): driver = webdriver.Chrome() try: # 一、輸入帳號密碼回車
        driver.implicitly_wait(3) driver.get('https://passport.cnblogs.com/user/signin') input_username = driver.find_element_by_id('input1') input_pwd = driver.find_element_by_id('input2') signin = driver.find_element_by_id('signin') input_username.send_keys(username) input_pwd.send_keys(password) signin.click() # 二、破解滑動認證
 crack(driver) time.sleep(10)  # 睡時間長一點,肯定登陸成功
    finally: driver.close() if __name__ == '__main__': login_cnblogs(username='linhaifeng',password='xxxx') 修訂版
修訂版

 

參考網址:https://www.cnblogs.com/linhaifeng/articles/7802150.html#toppython

有一句話感觸頗深,由於我也一直是這樣認爲的,好比說,,,數學,我一直都認爲數學只是一種學習的工具
我學習數學不少時候只是想去作某件事,或者想去了解這件事的原理,想把他弄通透罷了程序員

 

那麼網上的學習軟件,或者編程也好,本質不變,可是當程序員的性質變了,目的不變,web

 

引用:編程

也就不修改說明說明了

ps:破解圖片驗證碼的核心在於模擬人的行爲, 自筆者在老男孩授課以來,上述的破解思路已經分享給不少人, 相應地網絡上也已經有不少copy版, 極驗後臺的也在不斷學習用戶的破解行爲, 但歸根結底只要咱們將破解行爲模擬地足夠像人,極驗就拿咱們沒有辦法,

上面引用的話也在說,核心在於模擬人的行爲,那麼計算機的本質是什麼呢?不就是解法生產力。。。。解放思想嘛canvas

那麼爬蟲的本質是什麼?????   瀏覽器

我以爲如今我自學這些東西的目的在於什麼??網絡

或者說我想要達到什麼高度。。。。。app

若是不知道,我想能夠先放下了,想搞搞有目的有性質的東西——好比數學ide

相關文章
相關標籤/搜索