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日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line