无法使用session.post(URL,data=payload)登录到页面

2024-10-04 05:21:41 发布

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

我一直在尝试用python请求自动登录到一个页面,每小时下载一个文件,但我没有任何运气

这一页是:https://www.still-fds.com/fleetmanager/login/dercomaq

我使用的会话对象如下所示:

import requests

login_URL = 'https://www.still-fds.com/fleetmanager/login/dercomaq'

next_URL = 'https://www.still-fds.com/fleetmanager/pages/reports/vehicle.xhtml'

#(these arent the real username and password, I use the real ones in my code)
payload = {"j_idt181:username": "User", "j_idt181:password": "Password"}


with requests.Session() as ses:
    ses.get(login_URL) #I get a JSESSIONID cookie here
    
    ses.post(login_URL, data = payload) #I send the login request
    
    r = ses.get(next_URL) #I try accessing the next page after login

    with open('login-test.html', 'wb') as f: #writing the HTML i get back to a file so I can preview it
        f.write(r.content)

但是,当我检查下一页的预览时,它总是重定向/显示登录页 我还尝试发送一个更完整的负载,复制正常登录请求中发送的所有内容,如下所示

payload = {
'javax.faces.partial.ajax': 'true',
'javax.faces.source':'j_idt181:j_idt190',
'javax.faces.partial.execute': '@all',
'j_idt181:j_idt190': 'j_idt181:j_idt190',
'j_idt181': 'j_idt181',
'j_idt181:username': 'User',
'j_idt181:password': 'Password',
'javax.faces.ViewState': '-4453297688092219000:-1561371993877484606 '
}

我尝试过复制请求头:

#I replace the JSESSIONID cookie for the one that I'm given in the first get request
req_header = {
 'Accept': 'application/xml, text/xml, */*; q=0.01'
,'Accept-Encoding': 'gzip, deflate, br'
,'Accept-Language': 'es-ES,es;q=0.9,en-US;q=0.8,en;q=0.7'
,'Connection': 'keep-alive'
,'Content-Length': '287'
,'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
,'Cookie': 'JSESSIONID=U27BXdHsu+EXITX-QfUGkmqW; clientId=dercomaq' 
,'Faces-Request': 'partial/ajax'
,'Host': 'www.still-fds.com'
,'Origin': 'https://www.still-fds.com'
,'Referer': 'https://www.still-fds.com/fleetmanager/login/dercomaq'
,'Sec-Fetch-Dest': 'empty'
,'Sec-Fetch-Mode': 'cors'
,'Sec-Fetch-Site': 'same-origin'
,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'
,'X-Requested-With': 'XMLHttpRequest'
}

但是我从来没有得到过ckientId cookie(我尝试过手动给它一个clientId cookie,因为它总是'dercomaq',但也没有帮助)

当使用Selenium和ChromeDriver时,所有这些都非常容易,但由于web应用程序的限制,我不能使用它们


Tags: thehttpscomurlgetcookiewwwlogin