【问题标题】:Is there any solution for 404 not found in python requests?python请求中找不到404的解决方案吗?
【发布时间】:2020-01-03 20:05:36
【问题描述】:

我正在尝试登录,但出现 404 not found 错误。我尝试使用 cookie 登录,但两者均无效。我确定数据是正确的,我将它们提取出来。

我试图操纵标头数据,但没有任何效果。以及我试图改变用户代理也没有奏效。所以我在等待你的帮助。

import requests
def Headers():
    headerdata = {
        'Host': 'api.cathaypacific.com',
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Origin': 'https://www.asiamiles.com',
        'Upgrade-Insecure-Requests': '1',
        '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/76.0.3809.132 Safari/537.36',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-User': '?1',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Sec-Fetch-Site': 'cross-site',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'ar-EG,ar;q=0.9,en-US;q=0.8,en;q=0.7',
        'Content-Length': '368'}
    return headerdata
def PostData():
    data = 'response_type=code&scope=openid+offline_access&redirect_uri=https%3A%2F%2Fapi.asiamiles.com%2Fprofile%2Fapi%2Fv2%2FcreateSessionAndRedirect&client_id=de07768c-6e6d-4b48-81f3-dbf50618e598&login_url=https%3A%2F%2Fwww.asiamiles.com%2Fen%2Flogin.html&target_url=https%3A%2F%2Fwww.asiamiles.com%2Fen%2Faccount%2Faccount-overview.html&state=&username=1707617679&password=*123qweQWE'
    return data
def RequestEstablisher():
    url = 'https://api.cathaypacific.com/mlc2/authorize'
    opener = requests.sessions.session()
    temp = opener.post(url=url,data=PostData(),headers=Headers())
    print(temp.status_code,temp.reason)
    print(temp.is_redirect)
    print(temp.elapsed)
    print(temp.headers.get('Location'))
RequestEstablisher()

【问题讨论】:

  • 发布代码而不是您的代码图片,还有谁支持这个?
  • 首先您应该指定您正在使用的 Web 框架或 Web 服务器。 Python 本身是一种多用途语言,没有 Web 功能。它们都是由图书馆提供的。
  • 我建议让requests 为您编码您的数据,而不是尝试手动完成。
  • @Sahil 我已经调整过了。
  • @DavidMoseler 请解释更多。我应该添加或调整什么?

标签: python python-3.x https request http-status-code-404


【解决方案1】:

如果我排除标题,那么我会得到一个 OK 响应:

import requests

headers = {
    'Host': 'api.cathaypacific.com',
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'Origin': 'https://www.asiamiles.com',
    'Upgrade-Insecure-Requests': '1',
    '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/76.0.3809.132 Safari/537.36',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Sec-Fetch-Site': 'cross-site',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'ar-EG,ar;q=0.9,en-US;q=0.8,en;q=0.7',
    'Content-Length': '368',
}
post_data = {
    'response_type': 'code',
    'scope': 'openid+offline_access',
    'redirect_uri': 'https%3A%2F%2Fapi.asiamiles.com%2Fprofile%2Fapi%2Fv2%2FcreateSessionAndRedirect',
    'client_id': 'de07768c-6e6d-4b48-81f3-dbf50618e598',
    'login_url': 'https%3A%2F%2Fwww.asiamiles.com%2Fen%2Flogin.html',
    'target_url': 'https%3A%2F%2Fwww.asiamiles.com%2Fen%2Faccount%2Faccount-overview.html',
    'state': '',
    'username': '1707617679',
    'password': '*123qweQWE',
}
url = 'https://api.cathaypacific.com/mlc2/authorize'

opener = requests.sessions.session()
temp = opener.post(
    url=url,
    # headers=headers,
    data=post_data)

print(temp.status_code, temp.reason)
print(temp.is_redirect)
print(temp.elapsed)
print(temp.headers.get('Location'))

输出:

200 OK
False
0:00:00.794888
None

【讨论】:

  • 感谢您的回答。但我应该收到响应头 HTTP/1.1 302 Found。此外,Location 应通过返回 URL 内容错误或用户帐户概览页面的 URL 内容链接来满足登录数据是否正确。
猜你喜欢
  • 2023-01-24
  • 2017-10-21
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
相关资源
最近更新 更多