模拟登陆,带验证码。手动输入验证码,验证码图片下载不下来

*若价格不公道,可以让提问者在平台追加赏金哦,平台是您利益的保证

已完成
模拟登陆,带验证码。手动输入验证码,验证码图片下载不下来-学习中!!!
学习中!!! 4年前发布
悬赏:50.0 元

问题详情:分类: python

目标:模拟登陆,带验证码的。
说明:这是模拟登陆的链接:https://www.discuz.net/thread-3800749-1-1.html
问题:我的代码中涉及的是在内网,这个关系不大。现在存在问题:验证码图片下载不到本地???

import requests
session=requests.session()

# 第一步:
# 获取登录窗口中的loginhash和formhash
def get_login_window():
url = r'http://10.42.3.15:8081/member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login'
headers = {
'Host' : '10.42.3.15:8081',
'Connection' : 'close',
'User-Agent' : 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Mobile Safari/537.36',
'X-Requested-With' : 'XMLHttpRequest',
'Accept' : '*/*',
'Referer' : 'http://10.42.3.15:8081/forum.php',
'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : 'zh-CN,zh;q=0.9'

}

# 清空原来的headers
session.headers.clear()
# 更新headers
session.headers.update(headers)
r = session.get(url)

# 获取loginhash
p = r.text.find('loginhash') + len('loginhash') + 1
loginhash = r.text[p:p+5]

# 获取formhash
p = r.text.find('formhash') + len('formhash" value="')
formhash = r.text[p:p+8]
print(f'loginhash={loginhash}, formhash={formhash}')
return (loginhash, formhash)
# 没有问题的

# 第二步:
# 获取updata
def get_code_info():
url = r'http://10.42.3.15:8081/misc.php?mod=seccode&action=update&idhash=cSAsCMfGe&0.7587042042121976&modid=member::logging'
# r'http://10.42.3.15:8081/misc.php?mod=seccode&action=update&idhash=cSAL1tlFo&0.2430819204127488&modid=member::logging'
r = session.get(url)
p = r.text.find('update=')
print(f'p={p}')
update = r.text[p+7:p+12] #
print(f'update={update}')

p2 = r.text.find('idhash=')
idhash = r.text[p2+7:p2+16]
print(f'idhash={idhash}')

return update,idhash


# 第三步:
# 获取验证码
import urllib
import os,base64
import requests as req
from PIL import Image
from io import BytesIO

def get_code(update,idhash):

url = r'http://10.42.3.15:8081/misc.php?mod=seccode&update=' + update + '&idhash=' + idhash # 87618 cSAsCMfGe
response =req.get(url)
image = Image.open(BytesIO(response.content))
ls_f = base64.b64encode(BytesIO(response.content).read()).decode('utf-8')
imgdata = base64.b64decode(ls_f)

file = open('code.bmp', 'wb')
file.write(imgdata)
file.close()


print(f'url={url}')
headers = {
'Host' : '10.42.3.15:8081',
'Connection' : 'close',
'User-Agent' : 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Mobile Safari/537.36',
'X-Requested-With' : 'XMLHttpRequest',
'Accept' : 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8',
'Referer' : 'http://10.42.3.15:8081/forum.php',
'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : 'zh-CN,zh;q=0.9',
}

urllib.request.urlretrieve(url,'code.bmp')

session.headers.clear()
session.headers.update(headers)

r = session.get(url)
# text = r.text.encode(encoding='utf-8')
# print('text', text)
if(r.content[:5]==b'image'): # 这里判断验证码

# 保存验证码图片
file = open('code.jpg', 'wb')
file.write(r.content)
file.close()
else:
# 错误打印
# r.encoding = 'GBK'
print(f'error') # ={r.text}



# 第四步:
# 通过人工识别验证码
def check_code(code):
url = r'http://10.42.3.15:8081/misc.php?mod=seccode&action=check&inajax=1&modid=member::logging&idhash=cSAsCMfGe&secverify=' + code # 2ybg,这就是他妈的验证码

headers = {
'Host': '10.42.3.15:8081',
'Connection': 'close',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Mobile Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Accept': '*/*',
'Referer': 'http://10.42.3.15:8081/forum.php',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9'
}

session.headers.clear()
session.headers.update(headers)
r = session.get(url)

return r.text

# 第五步
# 模拟登录
def login(loginhash, formhash,code, username,password):
url = r'http://10.42.3.15:8081/member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=' + loginhash + '&inajax=1' # LFnBt 五位

data = {
'formhash' : formhash,
'referer' : 'http://10.42.3.15:8081/forum.php',
'loginfield': 'username',
'username' : username,
'password' : password,
'questionid' : '0',
'answer': '',
'seccodehash' : 'cSAsCMfGe',
'seccodemodid' : 'member::logging',
'seccodeverify' : code
}

headers = {
'Host': '10.42.3.15:8081',
'Connection': 'close',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Mobile Safari/537.36',
'Content-Length' : '410',
'Cache-Control' : 'no-store, private, post-check=0, pre-check=0, max-age=0',
'Origin' : 'http://10.42.3.15:8081',
'Upgrade-Insecure-Requests' : '1',
'Content-Type' : 'application/x-www-form-urlencoded',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Referer': 'http://10.42.3.15:8081/forum.php',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9'
}

session.headers.clear()
session.headers.update(headers)
r = session.post(url,data)
print(f'r={r.text}')


# 第六步:
# 主函数:模拟登录
# 1
(loginhash, formhash) = get_login_window()
# 2
update,idhash = get_code_info()

get_code(update, idhash) #
code = input("请输入验证码:")
# 3
check_code(code)
# 4
login(loginhash,formhash, code, username='xingda',password='xingda')

*若价格不公道,可以让提问者在平台追加赏金哦,平台是您利益的保证。你觉得当前的价格如何呢,奉上您珍贵的一票吧

虚高0人次 适中0人次 偏低0人次

分享海报会更快解决你的问题哦!分享海报

此处可发布评论

评论(0

暂无评论,快来写一下吧
客服QQ 1913284695