# encoding=utf8
import requests
import time
def get_verify_code(im, typeid):
verify_code = '****'
url = 'http://api.ruokuai.com/create.json'
params = {
'typeid': typeid,
'timeout': 60,
'username': '', # 用户名
'password': '', # 密码
'softid': '', # 软件Id
'softkey': '' # 软件Key
}
files = {
'image': ('a.jpg', im)
}
headers = {
'Connection': 'Keep-Alive',
'Expect': '100-continue',
'User-Agent': 'ben'
}
try:
resp = requests.post(url, data=params, files=files, headers=headers)
except Exception as e:
print 'get_verify_code error: ', e
return verify_code
try:
verify_code = resp.json().get('Result', '')
except Exception as e:
print 'get_verify_code failed: ', e
return verify_code
if not verify_code:
try:
print resp.text
except:
print 'verify code resp is None'
return verify_code
def main():
path = '' # 图片路径
with open(path, 'rb') as f:
content = f.read()
vcode = get_verify_code(content, 3040)
print '--- vcode: ', vcode
if __name__ == '__main__':
main()
说明:
软件Id和软件Key两个参数需要用户通过“开发者登录”后,在“软件管理”中添加一个软件,之后就可以得到一组softid和softkey,不过官方会有审核时间:
另外,上面代码中get_verify_code的第一个参数im既可以是以二进制保存的图片,也可以是通过http请求得到的图片的二进制源码,像这样:
def main():
url = '' # 请求图片的链接
resp = requests.get(url)
content = resp.content
vcode = get_verify_code(content, 3040)
print '--- vcode: ', vcode
if __name__ == '__main__':
main()
最后,代码中的3040是官方“编码Typeid”中的一种,具体可以参考这里:价格类型