目录
图片验证码识别:
一、百度OCR文字识别云服务
二、维普网获取图片验证码
三、维普网opencv+ocr识别验证码
四、维普网selenium登录并获取数据
滑块验证码:
五、猎聘网获取滑块验证码
六、猎聘网opencv计算滑动距离
七、猎聘网selenium模拟滑动轨迹
图片验证码识别:
一、百度OCR文字识别云服务
百度云-产品-搜索:文字识别-通用场景文字识别-立即使用-开通(实名认证)-免费尝鲜-创建新应用-调用服务-通用场景文字识别(高精度版)-复制相关代码
需要终端安装base64:pip install pybase64
import requests
import base64host = 'https://aip.baibubce.com/pauth/2.0/token?grant_type=client_credentials&client_id=[官网获取的AK]&client_secret=[官网获取的SK]'
response = requests.get(host)
if response:access_token = reponse.json()["access_token"]print(access_token)request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())params = {"image":img}
# access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" +access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:print(response.json()["words_result"][0]["words"])
二、维普网获取图片验证码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
import cv2
import requestsservice = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')
img.screenshot("./img/captcha.png")time.sleep(1)
driver.quit()
三、维普网opencv+ocr识别验证码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import json
import time
import cv2
import requests
import base64service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')
img.screenshot("./img/captcha.png")img = cv2.imread('./img/captcha.png', flags=cv2.IMREAD_GRAYSCALE)
thresh, img = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
cv2.imwrite("./img/captcha2.png", img)host = 'https://aip.baibubce.com/oauth/2.0/token?client_id=xxxxxx&client_secret=xxxxxx&grant_type=client_credentials'
payload = ""
headers = {'Content-Type': 'application/json','Accept': 'application/json'
}
response = requests.request("Post", url, headers=headers, data=payload)
access_token = reponse.json()["access_token"]request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
f = open('./img/captcha2.png', 'rb')
img = base64.b64encode(f.read())params = {"image":img}
request_url = request_url + "?access_token=" +access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:print(response.json()["words_result"][0]["words"])time.sleep(1)
driver.quit()
四、维普网selenium登录并获取数据
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import json
import time
import cv2
import requests
import base64service = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://my.cqvip.com/login")
time.sleep(1)username = driver.find_element(By.XPATH, '//*[@id="txtLoginUserName"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()password = driver.find_element(By.XPATH, '//*[@id="txtLoginPass"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()while driver.current_url == "http://my.cqvip.com/login":img = driver.find_element(By.XPATH, '//*[@id="verifycode"]')img.screenshot("./img/captcha.png")img = cv2.imread('./img/captcha.png', flags=cv2.IMREAD_GRAYSCALE)thresh, img = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)cv2.imwrite("./img/captcha2.png", img)host = 'https://aip.baibubce.com/oauth/2.0/token? client_id=xxxxxx&client_secret=xxxxxx&grant_type=client_credentials'payload = ""headers = {'Content-Type': 'application/json','Accept': 'application/json'}response = requests.request("Post", url, headers=headers, data=payload)access_token = reponse.json()["access_token"]request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"f = open('./img/captcha2.png', 'rb')img = base64.b64encode(f.read())params = {"image":img}request_url = request_url + "?access_token=" +access_tokenheaders = {'content-type': 'application/x-www-form-urlencoded'}response = requests.post(request_url, data=params, headers=headers)if response:result = response.json()["words_result"][0]["words"]print(result)cap = driver.find_element(By.XPATH, '//*[@id="validatecode"]')cap.clear()ActionChains(driver).pause(0.5).click(cap).send_keys(result).perform()submit = driver.find_element(By.XPATH, '//*[@id="btnAccountLogin"]')submit.click()time.sleep(4)print(driver.page_source)time.sleep(1)
driver.quit()
滑块验证码:
五、猎聘网获取滑块验证码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import cv2
import time
import requests
import numpyservice = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://www.liepin.com/")
time.sleep(1)select = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[2]/div/div[2]')
select.click()username = driver.find_element(By.XPATH, '//*[@id="login"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()password = driver.find_element(By.XPATH, '//*[@id="pwd"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()clickable = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[4]/div/label/span[1]/input')
ActionChains(driver).pause(0.5).click(clickable).perform()submit = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[3]/div/form/button')
ActionChains(driver).pause(0.5).click(submit).perform()time.sleep(2)
driver.switch_to.frame("tcaptcha_iframe")back = driver.find_element(By.XPATH, '//*[@id="slideBg"]').get_attribute("src")
res_back = requests.get(back)
with open("./img2/back.png", "wb") as f:f.write(res_back.content)front = driver.find_element(By.XPATH, '//*[@id="slideBlock"]').get_attribute("src")
res_front = requests.get(front)
with open("./img2/front.png", "wb") as f:f.write(res_front.content)time.sleep(3)
driver.quit()
六、猎聘网opencv计算滑动距离
import cv2
import numpyback = cv2.imread('./img2/back.png', flags=cv2.IMREAD_GRAYSCALE)
front = cv2.imread('./img2/front.png', flags=cv2.IMREAD_GRAYSCALE)front = front[24:front.shape[0]-24, 24:front.shape[0]-24]thresh, back = cv2.threshold(back, 110, 255, cv2.THRESH_BINARY)
thresh, front = cv2.threshold(front, 40, 255, cv2.THRESH_BINARY_INV)cv2.imwrite('./img2/back2.png', back)
cv2.imwrite('./img2/front2.png', front)match = cv2.matchTemplate(back, front, cv2.TM_CCORR_NORMED)
distance = cv2.minMaxLoc(match)[3][0]
print(distance)# cv2.imshow("back", back)
# cv2.imshow("front", front)# cv2.waitKey(0)
# cv2.destroyAllWindows()
七、猎聘网selenium模拟滑动轨迹
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import cv2
import time
import requests
import numpyservice = Service(executable_path="../_resources/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("http://www.liepin.com/")
time.sleep(1)select = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[2]/div/div[2]')
select.click()username = driver.find_element(By.XPATH, '//*[@id="login"]')
ActionChains(driver).pause(0.5).click(username).send_keys("xxxxx").perform()password = driver.find_element(By.XPATH, '//*[@id="pwd"]')
ActionChains(driver).pause(0.5).click(password).send_keys("xxxxx").perform()clickable = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[4]/div/label/span[1]/input')
ActionChains(driver).pause(0.5).click(clickable).perform()submit = driver.find_element(By.XPATH, '//*[@id="home-banner-login-container"]/div/div/div/div/div[3]/div/form/button')
ActionChains(driver).pause(0.5).click(submit).perform()time.sleep(2)
driver.switch_to.frame("tcaptcha_iframe")while driver.current_url == "https://www.liepin.com/":refresh = driver.find_element(By.XPATH, '//*[@id="reload"]/div')refresh.click()time.sleep(1)back = driver.find_element(By.XPATH, '//*[@id="slideBg"]').get_attribute("src")res_back = requests.get(back)with open("./img2/back.png", "wb") as f:f.write(res_back.content)front = driver.find_element(By.XPATH, '//*[@id="slideBlock"]').get_attribute("src")res_front = requests.get(front)with open("./img2/front.png", "wb") as f:f.write(res_front.content)back = cv2.imread('./img2/back.png', flags=cv2.IMREAD_GRAYSCALE)front = cv2.imread('./img2/front.png', flags=cv2.IMREAD_GRAYSCALE)front = front[24:front.shape[0]-24, 24:front.shape[0]-24]thresh, back = cv2.threshold(back, 110, 255, cv2.THRESH_BINARY)thresh, front = cv2.threshold(front, 40, 255, cv2.THRESH_BINARY_INV)cv2.imwrite('./img2/back2.png', back)cv2.imwrite('./img2/front2.png', front)match = cv2.matchTemplate(back, front, cv2.TM_CCORR_NORMED)distance = cv2.minMaxLoc(match)[3][0]# 341:滑动验证码图片的长, 680:下载后图片的长, 37:滑块现在距离滑动验证码图片最左端的距离distance = distance * 341 // 680 - 37print(distance)slider = driver.find_element(By.XPATH, '//*[@id="tcaptcha_drag_thumb"]')ActionChains(driver).pause(0.2).click_and_hold(slider).pause(0.2).move_by_offset(distance / 4, 5).perform() ActionChains(driver).pause(0.1).move_by_offset(distance / 2, -2).perform()ActionChains(driver).pause(0.1).move_by_offset(distance / 4, 3).release().perform()time.sleep(3)driver.get("https://www.liepin.com/")
time.sleep(1)
print(driver.page_source)time.sleep(3)
driver.quit()