云打码 古诗文网

Posted 追风zz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了云打码 古诗文网相关的知识,希望对你有一定的参考价值。

# 云打码  代码示例

import http.client, mimetypes, urllib, json, time, requests

class 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.html codetype = 1004 timeout = 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 requests
from lxml import etree
from urllib import request
def 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 result
url = 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).text
tree = 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": "[email protected]",
    "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 #暂时不处理cookie

print(code_text) #打印解析出的验证码
with open(./gushi.html,w,encoding=utf-8) as f:
    f.write(page_login)

print(结束)

 

以上是关于云打码 古诗文网的主要内容,如果未能解决你的问题,请参考以下文章

实现简单的模拟登录网站

Python3爬虫使用云打码识别验证码

Python学习--打码平台

07.验证码处理

爬虫学习 07.验证码处理

爬虫进阶验证码处理:打码平台的使用(反反爬)