本帖最后由 REXCOOL 于 2021-10-05 22:07 编辑
之前看教程学习用超级鹰,根据论坛师兄们指点尝试用百度的智能识别,可以实现,但是成功率不高 零基础小白自学中,求指点!
目标网站:https://www.gushiwen.cn/ #baidu-aip 智能识别验证码[/font][/font] from aip import AipOcr import requests from lxml import etree import time """ 你的 APPID AK SK """ APP_ID = 'APP_ID' API_KEY = 'API_KEY' SECRET_KEY = 'SECRET_KEY' #识别验证码图片 def tranformImgCode(imgPath): client = AipOcr(APP_ID, API_KEY, SECRET_KEY) im = open(imgPath, 'rb').read() return client.basicGeneral(im)["words_result"][0]["words"] #取验证码 t = int(round(time.time() * 1000)) #时间戳 session = requests.Session() #维持会话,可以让我们在跨请求时保存某些参数 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'} #获取验证码图片 url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx' #登陆地址 page_text = session.get(url=url,headers=headers).text #获取登陆地址内容 #解析验证码图片地址 tree = etree.HTML(page_text) img_src = 'https://so.gushiwen.cn'+tree.xpath('//img[@id="imgCode"]/@src')[0]+'?t='+str(t) VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value') VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value') #动态数据 #将验证码保存到本地 img_data= session.get(img_src,headers=headers).content with open(r'D:\代码保存\a.jpg','wb') as fp: fp.write(img_data) #识别验证码 code_text = tranformImgCode(r'D:\代码保存\a.jpg') print(code_text) login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx' data = { '__VIEWSTATE': VIEWSTATE, '__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR, 'from': 'http://so.gushiwen.cn/user/collect.aspx', 'email': '网页账号', 'pwd': '网页密码', 'code': code_text, 'denglu': '登录', } #点击登陆按钮发起请求,获取了登陆成功后对应的页码源码数据 page_text_login = session.post(url=login_url,headers=headers,data=data).text #保存网页 with open(r'D:\代码保存\gushiwen.html','w',encoding='utf-8') as fp: fp.write(page_text_login)
|