Python3 +scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30742.htmlphp

點觸驗證碼主要有點擊文字或者點擊圖片,例如html

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。python

而後下載python的Demo進行識別。web

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

下載下來添加在python文件當中,代碼以下:chrome

import requests  
from hashlib import md5  
  
# 定義超級鷹類  
class Chaojiying(object):  
    # 一、初始化超級鷹數據信息  
    def __init__(self,username,password,soft_id):  
  
        # 超級鷹用戶名  
        self.username = username  
  
        # 超級鷹密碼  
        self.password = md5(password.encode("utf-8")).hexdigest()  
  
        # 超級鷹軟件ID  
        self.soft_id = soft_id  
        self.base_params = {  
            "user":self.username,  
            "pass2":self.password,  
            "softid":self.soft_id,  
        }  
        self.headers = {  
            'Connection': 'Keep-Alive',  
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
        }  
  
    # 二、提交相關信息到超級鷹後臺驗證  
    def post_pic(self,im,codetype):  
        """  
        將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
        :param im: 圖片對象  
        :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
        :return:  
        """  
        # 超級鷹驗證碼代號提交字典  
        params = {"codetype":codetype}  
  
        # 將base_params更新到params內  
        params.update(self.base_params)  
  
        # 外界SDK,固定不能改  
        files = {"userfile":("ccc.jpg",im)}  
  
        # 超級鷹後臺驗證結果連接  
        url = "http://upload.chaojiying.net/Upload/Processing.php"  
  
        # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
        r = requests.post(url=url,data=params,files=files,headers=self.headers)  
        return r.json()  
  
    # 三、驗證出錯回調  
    def report_error(self,im_id):  
        """  
        發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
        :param im_id: 報錯題目的圖片ID  
        :return:  
        """  
        params = {"id":im_id}  
        params.update(self.base_params)  
        url = "http://upload.chaojiying.net/Upload/ReportError.php"  
        r = requests.post(url=url,data=params,headers=self.headers)  
        return r.json()

根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。json

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

首先咱們須要獲取到驗證碼瀏覽器

# 獲取驗證碼圖片  
    def get_crop_image(self):  
        time.sleep(random.random() + 1)  
        print('正在獲取iframe註冊頁')  
  
        # 獲取到iframe  
        iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  
  
        # 獲取iframe註冊頁面跳轉的src  
        iframe_src = iframe.get_attribute("src")  
        print("已經獲取到iframe鏈接:\n",iframe_src)  
  
        # 再次打開這個src  
        self.browser.get(iframe_src)  
  
        # 獲取驗證碼  
        element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  
  
        location = element.location                     # 獲取驗證碼的位置  
        size = element.size                             # 獲取驗證碼的大小  
        print(location, size)  
  
        # 獲取驗證碼圖片的上下左右位置  
        top = location['y']  
        bottom = location['y'] + size['height']  
        left = location['x']  
        right = location['x'] + size['width']  
        print('驗證碼位置', top, bottom, left, right)  
        return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼

而後獲取到驗證碼後須要驗證app

# 驗證碼驗證函數  
    def revalidation(self, axis, element):  
        """  
        :param axis: (left,top,right,bottom)  
        :param element: 驗證碼圖片  
        :return:  
        """  
        self.screen_shot()                                  # 調用截屏函數  
        if not self.screen_shot():                          # 判斷截屏是否成功  
            return '截圖失敗'  
        time.sleep(1)  
        new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
        new_image.show()                                    # 顯示截屏後的驗證碼  
        click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
        print(self.click_submit(click_coordinates, axis, element))  
        return '點擊驗證結束'

同時須要對驗證碼的屏幕進行截屏dom

1    # 屏幕截圖函數  
2     def screen_shot(self):  
3         time.sleep(2)  
4         self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
5         return True

獲取到截屏後須要對截屏進行剪切scrapy

# 屏幕截圖圖片剪切驗證碼  
    def shear_image(self,axis):  
        """  
        :param axis: (left,top,right,bottom)  
        :return:  
        """  
        im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
        new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
        new_image.save('new_img.png')               # 保存圖片  
        return new_image

剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

# 驗證碼上傳到超級鷹平臺驗證  
    def upload_picture(self,img):  
        """  
        :param img: 截屏後獲得的驗證碼圖片  
        :return:  
        """  
        image = img  
        byte_array = BytesIO()  
        image.save(byte_array, format('png'))  
  
        # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
        result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
        print(result)  
        if '無可用題分' in result['err_str']:  
            print('題分已經不足請充值!')  
        else:  
            print("題分充足,可使用題分驗證!")  
  
        # 獲取超級鷹驗證的座標位置  
        pic_str = result['pic_str']  
  
        # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
        pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
        for i in pic_list:  
            print(i)  
        print(pic_list)  
        return pic_list

最後就是點擊提交按鈕進行驗證啦

# 點擊提交按鈕  
    def click_submit(self, coordinates, axis, element):  
        """  
        :param coordinates: 文字的座標位置  
        :param axis: (left,top,right,bottom)  
        :param element: 驗證碼圖片  
        :return:  
        """  
        print('點擊開始')  
  
        # 遍歷座標位置  
        for location in coordinates:  
            # 移動鼠標到座標位置進行點擊文字並執行  
            ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
            time.sleep(random.random() + 1.8)  
  
        # 獲取提交按鈕並點擊提交  
        submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
        submission.click()  
        time.sleep(1)  
  
        # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
        if 'pw_submit_disabled' in submission.get_attribute('class'):  
            return '點擊成功'  
        else:  
            time.sleep(2)  
            print('正在從新識別新的驗證碼')  
            self.revalidation(axis, element)        # 回調驗證  
            return '點擊成功'

下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

第二:驗證碼截屏            

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

第三:點擊驗證

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

最後結合看看點擊提交按鈕的結果了

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

點擊提交按鈕以後class增長了pw_submit_disabled

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

因此纔有了它

Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別

最後附上源代碼

# -*- coding:utf-8 -*-  
import requests  
from hashlib import md5  
  
# 定義超級鷹類  
class Chaojiying(object):  
    # 一、初始化超級鷹數據信息  
    def __init__(self,username,password,soft_id):  
  
        # 超級鷹用戶名  
        self.username = username  
  
        # 超級鷹密碼  
        self.password = md5(password.encode("utf-8")).hexdigest()  
  
        # 超級鷹軟件ID  
        self.soft_id = soft_id  
        self.base_params = {  
            "user":self.username,  
            "pass2":self.password,  
            "softid":self.soft_id,  
        }  
        self.headers = {  
            'Connection': 'Keep-Alive',  
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
        }  
  
    # 二、提交相關信息到超級鷹後臺驗證  
    def post_pic(self,im,codetype):  
        """  
        將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
        :param im: 圖片對象  
        :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
        :return:  
        """  
        # 超級鷹驗證碼代號提交字典  
        params = {"codetype":codetype}  
  
        # 將base_params更新到params內  
        params.update(self.base_params)  
  
        # 外界SDK,固定不能改  
        files = {"userfile":("ccc.jpg",im)}  
  
        # 超級鷹後臺驗證結果連接  
        url = "http://upload.chaojiying.net/Upload/Processing.php"  
  
        # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
        r = requests.post(url=url,data=params,files=files,headers=self.headers)  
        return r.json()  
  
    # 三、驗證出錯回調  
    def report_error(self,im_id):  
        """  
        發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
        :param im_id: 報錯題目的圖片ID  
        :return:  
        """  
        params = {"id":im_id}  
        params.update(self.base_params)  
        url = "http://upload.chaojiying.net/Upload/ReportError.php"  
        r = requests.post(url=url,data=params,headers=self.headers)  
        return r.json()  
  
  
  
import time  
import random  
from io import BytesIO  
from PIL import Image  
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  
  
CHAOJIYING_USERNAME = "*******"      # 超級鷹用戶名  
CHAOJIYING_PASSWORD = "*******"      # 超級鷹密碼  
CHAOJIYING_SOFT_ID = 123456          # 超級鷹軟件ID  
CHAOJIYING_KIND = 9004               # 超級鷹驗證碼類型  
  
  
# 定義YY直播類  
class Touch_point_verification():  
    # 四、初始化selenium數據信息  
    def __init__(self):  
        self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"  
        self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")  
        self.browser.maximize_window()      # 設置瀏覽器爲最大窗口  
        self.wait = WebDriverWait(self.browser,timeout=5)  
  
        # 調用超級鷹類,發送信息  
        self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  
  
    def __del__(self):  
        self.browser.close()        # 關閉瀏覽器  
  
  
    # 獲取驗證碼圖片  
    def get_crop_image(self):  
        time.sleep(random.random() + 1)  
        print('正在獲取iframe註冊頁')  
  
        # 獲取到iframe  
        iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  
  
        # 獲取iframe註冊頁面跳轉的src  
        iframe_src = iframe.get_attribute("src")  
        print("已經獲取到iframe鏈接:\n",iframe_src)  
  
        # 再次打開這個src  
        self.browser.get(iframe_src)  
  
        # 獲取驗證碼  
        element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  
  
        location = element.location                     # 獲取驗證碼的位置  
        size = element.size                             # 獲取驗證碼的大小  
        print(location, size)  
  
        # 獲取驗證碼圖片的上下左右位置  
        top = location['y']  
        bottom = location['y'] + size['height']  
        left = location['x']  
        right = location['x'] + size['width']  
        print('驗證碼位置', top, bottom, left, right)  
        return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  
  
  
    # 屏幕截圖函數  
    def screen_shot(self):  
        time.sleep(2)  
        self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
        return True  
  
  
    # 屏幕截圖圖片剪切驗證碼  
    def shear_image(self,axis):  
        """  
        :param axis: (left,top,right,bottom)  
        :return:  
        """  
        im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
        new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
        new_image.save('new_img.png')               # 保存圖片  
        return new_image  
  
  
    # 驗證碼上傳到超級鷹平臺驗證  
    def upload_picture(self,img):  
        """  
        :param img: 截屏後獲得的驗證碼圖片  
        :return:  
        """  
        image = img  
        byte_array = BytesIO()  
        image.save(byte_array, format('png'))  
  
        # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
        result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
        print(result)  
        if '無可用題分' in result['err_str']:  
            print('題分已經不足請充值!')  
        else:  
            print("題分充足,可使用題分驗證!")  
  
        # 獲取超級鷹驗證的座標位置  
        pic_str = result['pic_str']  
  
        # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
        pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
        for i in pic_list:  
            print(i)  
        print(pic_list)  
        return pic_list  
  
  
    # 點擊提交按鈕  
    def click_submit(self, coordinates, axis, element):  
        """  
        :param coordinates: 文字的座標位置  
        :param axis: (left,top,right,bottom)  
        :param element: 驗證碼圖片  
        :return:  
        """  
        print('點擊開始')  
  
        # 遍歷座標位置  
        for location in coordinates:  
            # 移動鼠標到座標位置進行點擊文字並執行  
            ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
            time.sleep(random.random() + 1.8)  
  
        # 獲取提交按鈕並點擊提交  
        submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
        submission.click()  
        time.sleep(1)  
  
        # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
        if 'pw_submit_disabled' in submission.get_attribute('class'):  
            return '點擊成功'  
        else:  
            time.sleep(2)  
            print('正在從新識別新的驗證碼')  
            self.revalidation(axis, element)        # 回調驗證  
            return '點擊成功'  
  
  
    # 驗證碼驗證函數  
    def revalidation(self, axis, element):  
        """  
        :param axis: (left,top,right,bottom)  
        :param element: 驗證碼圖片  
        :return:  
        """  
        self.screen_shot()                                  # 調用截屏函數  
        if not self.screen_shot():                          # 判斷截屏是否成功  
            return '截圖失敗'  
        time.sleep(1)  
        new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
        new_image.show()                                    # 顯示截屏後的驗證碼  
        click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
        print(self.click_submit(click_coordinates, axis, element),"========================")  
        return '點擊驗證結束'  
  
  
    # 驗證開始  
    def Touch_point_main(self):  
        self.browser.get(self.url)              # 打開網頁  
        axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
        self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)  
  
  
if __name__ == '__main__':  
    crack = Touch_point_verification()  
    crack.Touch_point_main()
```感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30742.html

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main':
crack = Touch_point_verification()
crack.Touch_point_main()

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main':
crack = Touch_point_verification()
crack.Touch_point_main()

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main':
crack = Touch_point_verification()
crack.Touch_point_main()

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main':
crack = Touch_point_verification()
crack.Touch_point_main()

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main':
crack = Touch_point_verification()
crack.Touch_point_main()

點觸驗證碼主要有點擊文字或者點擊圖片,例如

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/878eeee1eaee423896466fded061e729)

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/629962af285b45cfa7ddf3e49ff53a80)

如何經過點擊圖片當中的文字或者圖片進行識別呢?那麼這裏介紹一下超級鷹平臺,須要註冊登陸有一個超級鷹帳號和密碼。

而後下載python的Demo進行識別。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f3181f81349f499388c61f4ecce488b6)

下載下來添加在python文件當中,代碼以下:

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()
根據識別的元素選擇超級鷹驗證碼類型,因爲識別的是三個文字,因此選擇9004,識別多個。

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/c068093b0e7e4d92a9c11c8d28eaf32b)

首先咱們須要獲取到驗證碼

獲取驗證碼圖片

def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼
而後獲取到驗證碼後須要驗證

驗證碼驗證函數

def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element))  
    return '點擊驗證結束'
同時須要對驗證碼的屏幕進行截屏

1 # 屏幕截圖函數
2 def screen_shot(self):
3 time.sleep(2)
4 self.browser.save_screenshot('yy.png') # 保存截屏圖片
5 return True

獲取到截屏後須要對截屏進行剪切

屏幕截圖圖片剪切驗證碼

def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image
剪切好了後須要將驗證碼上傳到超級鷹進行驗證,返回驗證結果

驗證碼上傳到超級鷹平臺驗證

def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list
最後就是點擊提交按鈕進行驗證啦

點擊提交按鈕

def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'
下面就用該平臺識別一下驗證碼,看看效果如何。

第一:屏幕截圖

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/3187b4945f1f4d669b4db632fc9690ed)

第二:驗證碼截屏            

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p3.pstatp.com/large/pgc-image/8ade1cb2a4ec4396855e05b324c14b7c)

第三:點擊驗證

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/7bfe574b6ab44513abccc0c0f1ffe1cb)

最後結合看看點擊提交按鈕的結果了

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p9.pstatp.com/large/pgc-image/9a085c96112644929a17ddac388fcd84)

注意:因爲點擊「提交」按鈕是網頁當中的class會發生變化,如提交以前的class=「pw_submit」:

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/08d378ccb1a6443dae49f0cc19f0f990)

點擊提交按鈕以後class增長了pw_submit_disabled

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/0c813db458b64f2f94dfb6fb19fd6008)

因此纔有了它

![Python+scrapy爬蟲識別驗證碼(二)點觸驗證碼識別](http://p1.pstatp.com/large/pgc-image/f00af342a4fa4294b67b7803462b71fb)

最後附上源代碼

-- coding:utf-8 --

import requests
from hashlib import md5

定義超級鷹類

class Chaojiying(object):
# 一、初始化超級鷹數據信息
def init(self,username,password,soft_id):

# 超級鷹用戶名  
    self.username = username  

    # 超級鷹密碼  
    self.password = md5(password.encode("utf-8")).hexdigest()  

    # 超級鷹軟件ID  
    self.soft_id = soft_id  
    self.base_params = {  
        "user":self.username,  
        "pass2":self.password,  
        "softid":self.soft_id,  
    }  
    self.headers = {  
        'Connection': 'Keep-Alive',  
        'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',  
    }  

# 二、提交相關信息到超級鷹後臺驗證  
def post_pic(self,im,codetype):  
    """  
    將圖片對象和相關信息傳到超級鷹後臺進行驗證,並返回驗證json  
    :param im: 圖片對象  
    :param codetype: 驗證碼代號  參考http://www.chaojiying.com/price.html  
    :return:  
    """  
    # 超級鷹驗證碼代號提交字典  
    params = {"codetype":codetype}  

    # 將base_params更新到params內  
    params.update(self.base_params)  

    # 外界SDK,固定不能改  
    files = {"userfile":("ccc.jpg",im)}  

    # 超級鷹後臺驗證結果連接  
    url = "http://upload.chaojiying.net/Upload/Processing.php"  

    # post請求超級鷹並把全部信息發送給超級鷹後臺進行驗證,並返回json  
    r = requests.post(url=url,data=params,files=files,headers=self.headers)  
    return r.json()  

# 三、驗證出錯回調  
def report_error(self,im_id):  
    """  
    發生錯誤是回調。若是驗證碼識別錯誤調用此方法返回題分  
    :param im_id: 報錯題目的圖片ID  
    :return:  
    """  
    params = {"id":im_id}  
    params.update(self.base_params)  
    url = "http://upload.chaojiying.net/Upload/ReportError.php"  
    r = requests.post(url=url,data=params,headers=self.headers)  
    return r.json()

import time
import random
from io import BytesIO
from PIL import Image
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

CHAOJIYING_USERNAME = "" # 超級鷹用戶名
CHAOJIYING_PASSWORD = "
" # 超級鷹密碼
CHAOJIYING_SOFT_ID = 123456 # 超級鷹軟件ID
CHAOJIYING_KIND = 9004 # 超級鷹驗證碼類型

定義YY直播類

class Touch_point_verification():
# 四、初始化selenium數據信息
def init(self):
self.url = "https://aq.yy.com/p/reg/account.do?appid=&url=&fromadv=udbclsd_r"
self.browser = webdriver.Chrome(executable_path=r"D:\spider\chromedriver.exe")
self.browser.maximize_window() # 設置瀏覽器爲最大窗口
self.wait = WebDriverWait(self.browser,timeout=5)

# 調用超級鷹類,發送信息  
    self.chaojiying = Chaojiying(CHAOJIYING_USERNAME,CHAOJIYING_PASSWORD,CHAOJIYING_SOFT_ID)  

def __del__(self):  
    self.browser.close()        # 關閉瀏覽器  


# 獲取驗證碼圖片  
def get_crop_image(self):  
    time.sleep(random.random() + 1)  
    print('正在獲取iframe註冊頁')  

    # 獲取到iframe  
    iframe = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div/iframe')  

    # 獲取iframe註冊頁面跳轉的src  
    iframe_src = iframe.get_attribute("src")  
    print("已經獲取到iframe鏈接:\n",iframe_src)  

    # 再次打開這個src  
    self.browser.get(iframe_src)  

    # 獲取驗證碼  
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="mPickWords"]/div[1]')))  

    location = element.location                     # 獲取驗證碼的位置  
    size = element.size                             # 獲取驗證碼的大小  
    print(location, size)  

    # 獲取驗證碼圖片的上下左右位置  
    top = location['y']  
    bottom = location['y'] + size['height']  
    left = location['x']  
    right = location['x'] + size['width']  
    print('驗證碼位置', top, bottom, left, right)  
    return (left,top,right,bottom),element          # 返回驗證碼圖片的位置和驗證碼  


# 屏幕截圖函數  
def screen_shot(self):  
    time.sleep(2)  
    self.browser.save_screenshot('yy.png')      # 保存截屏圖片  
    return True  


# 屏幕截圖圖片剪切驗證碼  
def shear_image(self,axis):  
    """  
    :param axis: (left,top,right,bottom)  
    :return:  
    """  
    im = Image.open('yy.png')                   # 打開屏幕截屏圖片  
    new_image = im.crop(axis)                   # 根據左上右下位置進行剪切  
    new_image.save('new_img.png')               # 保存圖片  
    return new_image  


# 驗證碼上傳到超級鷹平臺驗證  
def upload_picture(self,img):  
    """  
    :param img: 截屏後獲得的驗證碼圖片  
    :return:  
    """  
    image = img  
    byte_array = BytesIO()  
    image.save(byte_array, format('png'))  

    # 調用超級鷹類下的信息驗證,並讀取驗證碼圖片的全部內容,超級鷹返回驗證字典  
    result = self.chaojiying.post_pic(byte_array.getvalue(), CHAOJIYING_KIND)  
    print(result)  
    if '無可用題分' in result['err_str']:  
        print('題分已經不足請充值!')  
    else:  
        print("題分充足,可使用題分驗證!")  

    # 獲取超級鷹驗證的座標位置  
    pic_str = result['pic_str']  

    # '192,68|30,149|123,129'  將驗證返回的結果列表化獲得[['192', '68'], ['30', '149'], ['123', '129']]  
    pic_list = [[i for i in x.split(',')] for x in pic_str.split('|')]  
    for i in pic_list:  
        print(i)  
    print(pic_list)  
    return pic_list  


# 點擊提交按鈕  
def click_submit(self, coordinates, axis, element):  
    """  
    :param coordinates: 文字的座標位置  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    print('點擊開始')  

    # 遍歷座標位置  
    for location in coordinates:  
        # 移動鼠標到座標位置進行點擊文字並執行  
        ActionChains(self.browser).move_to_element_with_offset(element, location[0], location[1]).click().perform()  
        time.sleep(random.random() + 1.8)  

    # 獲取提交按鈕並點擊提交  
    submission = self.browser.find_element_by_xpath('//*[@id="mPickWords"]/div[2]/span[4]')  
    submission.click()  
    time.sleep(1)  

    # 判斷點擊提交按鈕後是否存在class屬性,若是存在則驗證成功,不然從新識別  
    if 'pw_submit_disabled' in submission.get_attribute('class'):  
        return '點擊成功'  
    else:  
        time.sleep(2)  
        print('正在從新識別新的驗證碼')  
        self.revalidation(axis, element)        # 回調驗證  
        return '點擊成功'  


# 驗證碼驗證函數  
def revalidation(self, axis, element):  
    """  
    :param axis: (left,top,right,bottom)  
    :param element: 驗證碼圖片  
    :return:  
    """  
    self.screen_shot()                                  # 調用截屏函數  
    if not self.screen_shot():                          # 判斷截屏是否成功  
        return '截圖失敗'  
    time.sleep(1)  
    new_image = self.shear_image(axis)                  # 調用截屏圖片剪切函數  
    new_image.show()                                    # 顯示截屏後的驗證碼  
    click_coordinates = self.upload_picture(new_image)  # 將截好的驗證碼圖片上傳超級鷹平臺驗證  
    print(self.click_submit(click_coordinates, axis, element),"========================")  
    return '點擊驗證結束'  


# 驗證開始  
def Touch_point_main(self):  
    self.browser.get(self.url)              # 打開網頁  
    axis, element = self.get_crop_image()   # 獲取驗證碼圖片  
    self.revalidation(axis, element)        # 進行驗證(參數爲位置和驗證碼圖片)

if name == 'main': crack = Touch_point_verification() crack.Touch_point_main()

相關文章
相關標籤/搜索