weixin_55452422 2021-12-23 10:09 采纳率: 83.3%
浏览 20
已结题

为什么打印的是登录页面的html,而不是登录后首页的html


import requests
import time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# 验证码的url地址
new_time = time.time() * 1000
image_url = "https://passport.tuniu.com/ajax/captcha/v/%s" % (new_time)
image_headers = {
    'Host': 'passport.tuniu.com',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
    'sec-ch-ua-mobile': '?0',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
    'sec-ch-ua-platform': '"Windows"',
    'accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'no-cors',
    'sec-fetch-dest': 'image',
    'referer': 'https://passport.tuniu.com/login?origin=https://www.tuniu.com/ssoConnect',
    'accept-language': 'zh-CN,zh;q=0.9'
}

# 定义一个session对象
req = requests.Session()
#通过这个对象调用get请求,响应获取cooki并保存验证码图片
cvalcod=req.get(image_url,headers=image_headers,verify=False)
f=open('验证码.jpg','wb') # 以字节流形式写入并保存
f.write(cvalcod.content)
f.flush()
f.close()
# 等待2秒
time.sleep(2)

# 登录请求的地址
login_url = "https://passport.tuniu.com/login?origin=https://www.tuniu.com/ssoConnect"
# 登录后的首页地址
url = 'https://i.tuniu.com/'
# 请求头
headers = {
    'Host': 'passport.tuniu.com',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'upgrade-insecure-requests': '1',
    'origin': 'https://passport.tuniu.com',
    'content-type': 'application/x-www-form-urlencoded',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
    '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',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'referer': 'https://passport.tuniu.com/login?origin=https://www.tuniu.com/ssoConnect',
    'accept-language': 'zh-CN,zh;q=0.9'
}
# 输入你的手机号,密码,验证码
tel=int(input('请输入手机号'))
password=input('请输入密码')
identify_code=input('请输入验证码')

data={
    'isWeak': 0,
    'login_type': 'P-N',
    'intlCode': '',
    'username': tel,
    'password': 'password',
    'identify_code': identify_code
}

# 发起登录请求
res = req.post(url=login_url, headers=headers, data=data, verify=False)
# print(res.content.decode('utf-8'))
html=req.get(url,verify=False)
print(html.content.decode())
  • 写回答

1条回答 默认 最新

  • 陈年椰子 2021-12-23 10:55
    关注

    应该是自动登录没成功吧。 验证码输入错误, 页面有提示码?

    print(res.content.decode('utf-8')
    这个内容是啥?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月15日
  • 已采纳回答 1月7日
  • 创建了问题 12月23日

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本