
CSDN博客
在开始之前应该配置好selenium+firefox
一、得到验证码图片
只有当鼠标移动到验证码上时,图片才会显示出来。因此我们可以采用鼠标悬停的的方式显示验证码,并得到图片保存在本地。
鼠标悬停拥有两种方式,一种是悬停到具体坐标,另一种是悬停到某一具体元素。我们选择前一种,原因有两点:由于最后识别出验证码时,需要点击具体坐标(每个字并不具有相对应的元素)。并且每一次鼠标执行都会在上一次的移动的坐标下再次移动(因此需要元素的坐标)。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Firefox()
driver.get('http://www.xxx.com')
ActionChains(driver).move_by_offset(762.5,447.76).perform()
img=driver.find_element_by_xpath('//*[@id="jcaptchaimage"]')
二、验证码识别
字体验证码采用腾讯云字体识别(传输的图片信息必须是图片url或者base64编码),返回识别字体及字体位置。字体位置返回的是相对图片验证码的字体四个顶点的位置,可以设置函数,得到字体中心距离坐标(相对验证码左上角)。
具体可看官方文档:https://cloud.tencent.com/product/ocr/developer
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
with open('1.png','rb') as f:
base64_data=base64.b64encode(f.read())
cred = credential.Credential("SecretId", "SecretKey")
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
req = models.GeneralBasicOCRRequest()
params = '{"ImageBase64":"'+s+'"}'
req.from_json_string(params)
resp = client.GeneralBasicOCR(req)
res=resp.to_json_string()
except TencentCloudSDKException as err:
def texts_and_locations(img_str):
img_str=json.loads(img_str)
TextDetections=img_str['TextDetections']
for t_d in TextDetections:
texts.append(t_d['DetectedText'])
x,y=cen_location(t_d['Polygon'])
cen_locations.append([x,y])
return (texts,cen_locations)
百度云智能云也可进行字体识别:
详细可看官方文档:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html
三、位置处理
由于每次坐标移动都是在上一次坐标的基础上进行移动,因此可以设置函数在每次移动过后,再相反移动一次,恢复原来坐标。然后将步骤二得到的中心位置进行点击。注意:由于中心坐标是相对于验证码左上角的位置,因此进行识别字体点击前必须先将坐标移动到验证码左上角(验证码左上角相对于页面的坐标)
def mo_click(driver,xy,left_click=True):
ActionChains(driver).move_by_offset(xy[0],xy[1]).click().perform()
ActionChains(driver).move_by_offset(xy[0],xy[1]).context_click().perform()
ActionChains(driver).move_by_offset(-xy[0],-xy[1]).perform()
def simulation_clicks(driver,by_cen_locations):
for xy in by_cen_locations:
「GetParty」
关注微信号,推送好文章
微信中长按图片即可关注