本次登录站点为古诗文网
URL:https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx
对于验证码的解析:采用云打码API接口实现 http://www.yundama.com
主页面实现代码:
import requests requests.packages.urllib3.disable_warnings() #禁用证书安装警告 from lxml import etree from verify import getCodeDate #获取网站的登录页面链接 url=‘https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx‘ S=requests.Session() user_agent={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"} page_text=requests.get(url=url,headers=user_agent,verify=False).text #登录页面文本内容 tree=etree.HTML(page_text) i_url=tree.xpath(‘//img[@id="imgCode"]/@src‘)[0] #解析出验证码的url code_img_url="https://so.gushiwen.org"+i_url #拼接完整验证码完整的URI #通过session去访问验证码url请求 为避免HTTPS中的ssl验证关闭验证 img_data=S.get(url=code_img_url,headers=user_agent,verify=False).content with open(‘./code/code.jpg‘,‘wb‘)as f: f.write(img_data) #验证码本地持久化存储 f.close() #调用云打码API去解析验证码文本内容 code_text=getCodeDate(‘bobo328410948‘,‘bobo328410948‘,‘./code/code.jpg‘,1004) print(code_text) login_url=‘https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx‘ data={ "__VIEWSTATE":"pnxXA5gIeJn+zyAzqHvjCGecHVkhFmDqa2YvRtvdp/9Yexq5Mngv8LjGEY4AmtwlXa36vUe0p4FxAvwDWIwYSiwv4g/7+oomRXa4hlKrhenT/ydAGJAQBzbXVbQ=", "__VIEWSTATEGENERATOR":"C93BE1AE", "from":"http://so.gushiwen.org/user/collect.aspx", "email":"123456@163.com", "pwd":"1159402553", "code":code_text, "denglu":"登录", } user_page=S.post(url=login_url,data=data,headers=user_agent).text print(user_page) with open(‘./gushi.html‘,‘w‘,encoding=‘utf-8‘)as f: f.write(user_page)
云打码的API接口代码:
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
对云打码API接口封装的函数代码:
from ydm import YDMHttp def getCodeDate(userName,pwd,codePath,codeType): # 用户名(普通用户) username = userName # 密码 password = pwd # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 6003 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = ‘1f4b564483ae5c907a1d34f8e2f2776c‘ # 图片文件 filename = codePath # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = codeType # 超时时间,秒 timeout = 20 result = None # 检查 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
原文地址:https://www.cnblogs.com/wen-kang/p/10952674.html
时间: 2024-10-07 22:50:33