博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
云打码 古诗文网
阅读量:6481 次
发布时间:2019-06-23

本文共 6239 字,大约阅读时间需要 20 分钟。

# 云打码  代码示例

import http.client, mimetypes, urllib, json, time, requestsclass YDMHttp:    apiurl = 'http://api.yundama.com/api.php'    username = ''    password = ''    appid = ''    appkey = ''    def __init__(self, username, password, appid, appkey):  #构造方法        self.username = username          self.password = password        self.appid = str(appid)        self.appkey = appkey    def request(self, fields, files=[]):        response = self.post_url(self.apiurl, fields, files)        response = json.loads(response)        return response        def balance(self):        data = {
'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['balance'] else: return -9001 def login(self): data = {
'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey} response = self.request(data) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['uid'] else: return -9001 def upload(self, filename, codetype, timeout): data = {
'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)} file = {
'file': filename} response = self.request(data, file) if (response): if (response['ret'] and response['ret'] < 0): return response['ret'] else: return response['cid'] else: return -9001 def result(self, cid): data = {
'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)} response = self.request(data) return response and response['text'] or '' def decode(self, filename, codetype, timeout): cid = self.upload(filename, codetype, timeout) if (cid > 0): for i in range(0, timeout): result = self.result(cid) if (result != ''): return cid, result else: time.sleep(1) return -3003, '' else: return cid, '' def report(self, cid): data = {
'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'} response = self.request(data) if (response): return response['ret'] else: return -9001 def post_url(self, url, fields, files=[]): for key in files: files[key] = open(files[key], 'rb'); res = requests.post(url, files=files, data=fields) return res.text######################################################################username = 'zhangc' #普通用户password = 'zc1991' #密码    appid = 7568 #开发者页面软件ID appkey = 'bbb4575c2c541e5fa4fe932ce86774ef' #秘钥 filename = './PythonHTTP调用示例/getimage.jpg' #解码图片路径 # 验证码类型 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.htmlcodetype = 1004timeout = 40 #超时时间 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) #打印余额 cid, result = yundama.decode(filename, codetype, timeout);# 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果    print('cid: %s, result: %s' % (cid, result)) #打印结果

# 古诗文网 模拟登陆 处理cookie

import requestsfrom lxml import etreefrom urllib import requestdef getCodeText(filename,codetype):    result = None    username    = 'zhangc'     password    = 'zc1991'                                appid       = 7568                                         appkey      = 'bbb4575c2c541e5fa4fe932ce86774ef'        filename    = filename                         codetype    = codetype    timeout     = 40                                     if (username == 'username'):        print('请设置好相关参数再测试')    else:        yundama = YDMHttp(username, password, appid, appkey)        uid = yundama.login();              balance = yundama.balance();              cid, result = yundama.decode(filename, codetype, timeout);    return resulturl = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'session = requests.Session()    # 创建session会话,处理cookie相关反爬机制headers = {    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}page_text = session.get(url=url,headers=headers).texttree = etree.HTML(page_text)code_src = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]  #https://so.gushiwen.org#request.urlretrieve(url=code_src,filename='./gushi.jpg')  #它有自己的cookie机制code_img = session.get(url=code_src,headers=headers).content with open('./gushi.jpg','wb') as fp:    fp.write(code_img)    __VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]    #动态加载__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]  #动态加载code_text = getCodeText('./gushi.jpg',1004)  #这里调用识别data = {    "__VIEWSTATE": __VIEWSTATE,    "__VIEWSTATEGENERATOR": __VIEWSTATEGENERATOR,    "from": "http://so.gushiwen.org/user/collect.aspx",    "email": "576951284@qq.com",    "pwd": "zhangchen",    "code": code_text,    "denglu": "登录",}login_url = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx'page_login = session.post(url=login_url,headers=headers,data=data).text #暂时不处理cookieprint(code_text) #打印解析出的验证码with open('./gushi.html','w',encoding='utf-8') as f:    f.write(page_login)print('结束')

 

转载于:https://www.cnblogs.com/zhangchen-sx/p/10853230.html

你可能感兴趣的文章
iOS自定制tabbar与系统的tabbar冲突,造成第一次点击各个item图片更换选中,第二次选中部分item图片不改变...
查看>>
我的路上
查看>>
DB2与oracle有什么区别
查看>>
创建一个多级文件目录
查看>>
Picasa生成图片幻灯片页面图文教程
查看>>
svn status 显示 ~xx
查看>>
常用HiveQL总结
查看>>
[转]使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger
查看>>
POJ 3311 Hie with the Pie(状压DP + Floyd)
查看>>
Security updates and resources
查看>>
DNS为什么通常都会设置为14.114.114.114
查看>>
Sqoop架构(四)
查看>>
golang copy函数
查看>>
《你有多少问题要请示》精华集粹
查看>>
打印图片
查看>>
SHOW CREATE DATABASE Syntax
查看>>
rsync常见问题及解决办法
查看>>
MySQL日期 专题
查看>>
C#中禁止程序多开
查看>>
分布式缓存Redis使用以及原理
查看>>