import requests from lxml import etree from codeclass import YDMHttp # 封装识别验证码图片下的函数 def getcodetext(imgpath,codetype): # 普通 用户名 username = \'bobo328410948\' # 普通 密码 password = \'bobo328410948\' # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 6003 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = \'1f4b564483ae5c907a1d34f8e2f2776c\' # 图片文件 :即将被识别验证码的路径 filename =imgpath # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = codetype # 超时时间,秒 timeout = 20 # 检查 if (username == \'username\'): print(\'请设置好相关参数再测试\') else: # 初始化 yundama = YDMHttp(username, password, appid, appkey) # 登陆云打码 uid = yundama.login(); print(\'uid: %s\' % uid) # 查询余额 balance = yundama.balance(); print(\'balance: %s\' % balance) # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果 cid, result = yundama.decode(filename, codetype, timeout); print(\'cid: %s, result: %s\' % (cid, result)) return result # 将验证码图片下载到本地 headers = { \'User-Agent\': \'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\' } # UA伪装 url = \'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx\' # 获取登陆首页源代码文件 page_text = requests.get(url=url,headers = headers).text # 解析验证码图片img中的src属性值 获取验证码的url tree = etree.HTML(page_text) img_code_src= tree.xpath(\'//*[@id="imgCode"]/@src\')[0] # 复制xpath代码 img_data = requests.get(url= url,headers = headers).content # 将验证码图片保存到本地 fp = open(\'./code.jpg\',\'wb\') fp.write(img_data) # 调用打码平台的示例程序进行验证码图片数据识别 code_text=getcodetext(\'./code.jdp\',1004) print(\'识别结果为:\',code_text)