有没有在python请求中找不到的404解决方案?

2024-04-26 23:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试登录到,但收到404 not found错误。我尝试用cookies和不使用cookies登录,但两者都不起作用。我确信这些数据是正确的,而且我是用计算机提取出来的。你知道吗

我试图操纵标题数据,但没有任何效果。以及我试图改变用户代理也没有工作。所以我在等你的帮助。你知道吗

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()

Tags: 数据comurldataapplicationdefhtmlsec
1条回答
网友
1楼 · 发布于 2024-04-26 23:32:50

如果我排除了头,那么我会得到一个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

相关问题 更多 >