这个需要自己注册一个账号:byzhang/root
进入这个接口:
https://www.showapi.com/apiGateway/view?apiCode=184
点击详情然后选择:
选择下载SDK
解压缩后放到lib目录下
详细代码如下:
import requests
from urllib import parse
#全局请求头
files = {}
headers = {}
body = {}
timeouts = {}
resHeader = {}
class ShowapiRequest:
def __init__(self, url, my_appId, my_appSecret):
self.url = url
self.my_appId = my_appId
self.my_appSecret = my_appSecret
body["showapi_appid"] = my_appId
body["showapi_sign"] = my_appSecret
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2427.7 Safari/537.36"
def addFilePara(self, key, value_url):
files[key] = open(r"%s" % (value_url), 'rb')
return self
def addHeadPara(self, key, value):
headers[key] = value
return self
def addBodyPara(self, key, value):
body[key] = value
return self
#设置连接时间和读取时间
def setTimeout(self, connecttimout, readtimeout):
timeouts["connecttimout"] = connecttimout
timeouts["readtimeout"] = readtimeout
return self
def get(self):
get_url = self.url + "?" + parse.urlencode(body)
if not timeouts:
res = requests.get(get_url, headers=headers)
else:
timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
res = requests.get(get_url, headers=headers, timeout=timeouts)
return res
def post(self):
if not timeouts:
res = requests.post(self.url, files=files, data=body, headers=headers)
else:
timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
res = requests.post(self.url, files=files, data=body, headers=headers, timeout=timeout)
return res
实例:
需要先下载个包
pip install requests
原始实例:
# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from lib.ShowapiRequest import ShowapiRequest
r = ShowapiRequest("http://route.showapi.com/184-4","my_appId","my_appSecret" )
r.addFilePara("image", "替换为你的文件")
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
print(res.text) # 返回信息
“my_appId”,"my_appSecret"需要找到密钥
进入万维网我的应用里
490656
da211c7555a647b4bc32c0ce71cf5f11
好 接下来是开始完善代码验证验证码
# python3.6.5
# 需要引入requests包 :运行终端->进入python/Scripts ->输入:pip install requests
from seelenium_project.lib.ShowapiRequest import ShowapiRequest
#通过后台AI算法来识别验证码
r = ShowapiRequest("http://route.showapi.com/184-4","490656","da211c7555a647b4bc32c0ce71cf5f11" ) #密钥和密码
r.addFilePara("image","test.jpg") #图片名称
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
print(res.text) # 返回信息
#取showapi_res_body中的result
body=res.json()['showapi_res_body']
print(body)
#取出验证码
print(body['Result']) #yf66j这个就是提取出来的验证码
运用到项目中见后面详情!!!
获取图片到验证
def get_code(driver):
# 获取验证码图片
t = time.time()
path = os.path.dirname(os.path.dirname(__file__)) + '\\screenshopts'
picture_name1 = path + '\\' + str(t) + '.png'
driver.save_screenshot(picture_name1)
ce = driver.find_element_by_xpath('/html/body/div[1]/div/form/div[6]/img')
left = ce.location['x']
top = ce.location['y']
right = ce.size['width'] + left
height = ce.size['height'] + top
dpr = driver.execute_script('return window.devicePixelRatio')
print(dpr)
im = Image.open(picture_name1)
img = im.crop((left*dpr, top*dpr, right*dpr, height*dpr))
t = time.time()
picture_name2 = path + '\\' + str(t) + '.png'
img.save(picture_name2) # 这里就是截取到的验证码图片
#这里是复杂验证码获取
r = ShowapiRequest("http://route.showapi.com/184-4", "290728", "1bd001f23c874581aac4db788a92c71d")
r.addFilePara("image", picture_name2)
r.addBodyPara("typeId", "34")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
print(res.text) # 返回信息
# 取showapi_res_body中的result
body = res.json()['showapi_res_body']
print(body)
# 取出验证码
print(body['Result'])
main函数里
#获取验证码
driver=webdriver.Chrome()
driver.get('http://localhost:8080/jpress/user/register')
driver.maximize_window()
util.get_code(driver)
print(util.get_code(driver))