day5 - 超級鷹驗證碼和B站滑動驗證

  • 導(dǎo)入超級鷹的包在項目下面

1. e21網(wǎng)站驗證碼識別

"""__author__= 雍新有"""
from io import BytesIO

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from PIL import Image

from chaojiying_Python.chaojiying import main1

browser = webdriver.Chrome()
browser.get('http://bm.e21cn.com/login/user')
wait = WebDriverWait(browser, 10)
# 將屏幕的寬高自定義恭金,或者執(zhí)行js實現(xiàn)拖拽(window.scrollTo(1000, 1000))
# browser.set_window_size(1500, 1300)


def screen_big_png():
    # 獲取整個窗口的圖片
    big_screen = browser.get_screenshot_as_png()
    # 保存  BytesIO -- 讀取二進制文件
    img = Image.open(BytesIO(big_screen))
    print(img)
    img.save('a1.png')
    return img


def get_position():
    # 顯示等待
    img = wait.until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="imgCheckCode"]'))
    )
    print(img.location)
    print(img.size)
    size = img.size
    location = img.location
    # 左上角定位
    x1 = location['x'] * 1.25
    y1 = location['y'] * 1.25
    # 右下角定位
    x2 = x1 + size['width']*1.28
    y2 = y1 + size['height']*1.28
    return (x1, y1, x2, y2)


def screen_small_png(big_png):
    # 先獲取驗證碼的位置,x和y
    x1, y1, x2, y2 = get_position()
    img = big_png.crop((x1, y1, x2, y2))
    img.save('a2.png')


if __name__ == '__main__':
    # 扣大圖
    big_png = screen_big_png()
    # 扣小圖
    screen_small_png(big_png)
    # 超級鷹校驗
    result = main1('a2.png')
    code = result['pic_str']
    print(code)
    # 模擬登陸
    # 顯示等待咙边,獲取

2. B站極驗驗證碼

"""__author__= 雍新有"""
import time
from io import BytesIO

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image


class BiliSpider():

    def __init__(self):
        self.browser = webdriver.Chrome()
        self.wait = WebDriverWait(self.browser, 30)
        self.url = 'https://passport.bilibili.com/login'
        self.username = 'coco'
        self.password = '123456'
        self.filename1 = 'big1.png'
        self.filename2 = 'big2.png'
        self.smallname1 = 's1.png'
        self.smallname2 = 's2.png'

    # def __del__(self):
    #     # 類執(zhí)行完后會自動調(diào)用這個函數(shù)
    #     self.browser.close()

    def login_open(self):
        # 打開B站登陸頁面,并輸入賬號密碼睬棚,最后點擊登陸按鈕
        self.browser.get(self.url)
        # 賬號輸入框
        name_input = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login-username"]'))
        )
        name_input.clear()
        name_input.send_keys(self.username)
        # 密碼輸入框
        password_input = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login-passwd"]'))
        )
        password_input.clear()
        password_input.send_keys(self.password)
        # 點擊登陸按鈕
        button = self.wait.until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="geetest-wrap"]/ul/li[5]/a[1]'))
        )
        button.click()
        # 目的是讓驗證碼加載出來抠忘,手動的睡眠幾秒
        time.sleep(3)

    def save_big_png(self, filename, smallname):
        # 實現(xiàn)截大圖
        img = self.browser.get_screenshot_as_png()
        img = Image.open(BytesIO(img))
        img.save(filename)
        # 截取小圖
        small_png = self.crop_png(img, smallname)
        return small_png

    def screen_png(self):
        # 截大圖
        # 橫向滾動
        # js = 'window.scrollTo(1000, 0)'
        # self.browser.execute_script(js)
        # 截取,保存有缺口的大圖, 返回小圖
        img_s1 = self.save_big_png(self.filename1, self.smallname1)
        # 隱藏驗證碼中的缺口,然后在截取
        js = 'document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display="block"'
        self.browser.execute_script(js)
        img_s2 = self.save_big_png(self.filename2, self.smallname2)
        return img_s1, img_s2

    def get_position(self):
        # 獲取左上角和右下角的橫縱坐標(biāo)位置
        chapter = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]'))
        )
        location = chapter.location
        size = chapter.size
        x1 = location['x']
        y1 = location['y']
        x2 = x1 + size['width']
        y2 = y1 + size['height']
        return x1, y1, x2, y2

    def crop_png(self, img, filename):
        # 截取小圖结耀,有缺口小圖和無缺口小圖
        x1, y1, x2, y2 = self.get_position()
        small_img = img.crop((x1, y1, x2, y2))
        small_img.save(filename)
        return small_img

    def compare_img(self, img1, img2, x, y):
        # 比較圖片像素點留夜,像素點相似返回True,否則False
        # getpixel((x, y)) , img1.load()[x, y] - 獲取圖片像素點的rgba值

        pix1 = img1.load()[x, y]
        pix2 = img2.load()[x, y]
        # 閾值 - 像素偏差
        a = 60
        if abs(pix1[0] - pix2[0]) < a and \
            abs(pix1[1] - pix2[1]) < a and \
            abs(pix1[2] - pix2[2]) < a and \
            abs(pix1[3] - pix2[3]) < a:
            # 兩個像素點相差不大
            return True
        return False

    def get_distance(self, img1, img2):
        # 計算兩張小圖的缺口距離
        # 比較兩張圖片的每一個像素點图甜,誤差不能超過某個閾值
        print(img1.size)
        left = 70
        # 遍歷小圖中橫坐標(biāo)58右邊的所有點
        for x in range(left, img1.size[0]):
            for y in range(img1.size[1]):
                # 比較2張小圖的像素點
                if not self.compare_img(img1, img2, x, y):
                    return x
        return left

    def slider_button(self, distance):
        # 拖動下面滑塊
        slider = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[6]/div/div[1]/div[2]/div[2]'))
        )
        action = ActionChains(self.browser)
        # 執(zhí)行點擊并抓住
        action.click_and_hold(slider).perform()
        print('==============')
        while distance > 0:
            print(distance)
            distance -= 2
            action.move_by_offset(xoffset=2, yoffset=0).perform()
            # 新建ActionChains對象防止累加位移
            action = ActionChains(self.browser)
            # time.sleep(0.2)
        action.release(slider).perform()

    def start(self):
        self.login_open()
        img_s1, img_s2 = self.screen_png()
        # 獲取兩張小圖的距離 -- 從圖中滑塊的左邊到陰影的左邊
        distance = self.get_distance(img_s1, img_s2) - 7
        print(distance)
        # 滑動滑塊
        self.slider_button(distance)


if __name__ == '__main__':
    # 扣有缺口圖和沒有缺口圖碍粥,對比兩張圖的像素點,找出拖拽的橫坐標(biāo)具则,實現(xiàn)拖拽即纲。
    spider = BiliSpider()
    spider.start()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市博肋,隨后出現(xiàn)的幾起案子低斋,更是在濱河造成了極大的恐慌,老刑警劉巖匪凡,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件膊畴,死亡現(xiàn)場離奇詭異,居然都是意外死亡病游,警方通過查閱死者的電腦和手機唇跨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來衬衬,“玉大人买猖,你說我怎么就攤上這事∽涛荆” “怎么了玉控?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長狮惜。 經(jīng)常有香客問我高诺,道長,這世上最難降的妖魔是什么碾篡? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任虱而,我火速辦了婚禮,結(jié)果婚禮上开泽,老公的妹妹穿的比我還像新娘牡拇。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布诅迷。 她就那樣靜靜地躺著佩番,像睡著了一般。 火紅的嫁衣襯著肌膚如雪罢杉。 梳的紋絲不亂的頭發(fā)上趟畏,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音滩租,去河邊找鬼赋秀。 笑死,一個胖子當(dāng)著我的面吹牛律想,可吹牛的內(nèi)容都是我干的猎莲。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼技即,長吁一口氣:“原來是場噩夢啊……” “哼著洼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起而叼,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤身笤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后葵陵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體液荸,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年脱篙,在試婚紗的時候發(fā)現(xiàn)自己被綠了娇钱。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡绊困,死狀恐怖文搂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情秤朗,我是刑警寧澤细疚,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站川梅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏然遏。R本人自食惡果不足惜贫途,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望待侵。 院中可真熱鬧丢早,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至农猬,卻和暖如春赡艰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斤葱。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工慷垮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人揍堕。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓料身,卻偏偏與公主長得像,于是被迫代替她去往敵國和親衩茸。 傳聞我的和親對象是個殘疾皇子芹血,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容