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()