![](/img/trans.png)
[英]How can I log into a website using Python Requests when website has Jquery for login?
[英]How can i login to website with python requests?
我想使用 python 請求登錄網站,相關網站是: https://www.subito.it/
另外我想知道如何確認程序能夠登錄。
這是我的代碼:
import requests
def login(email, password):
response = requests.get('https://www.subito.it/')
s = requests.session()
payload = {
'username': email,
'password': password,
'remember_me': "true",
'back': ""
}
response = s.post('https://areariservata.subito.it/hera-api/login', json=payload)
print(response.content)
print("Status code: ", response.status_code)
if response.ok:
print("All good!")
else:
print("Something went wrong...")
session = login('MY_EMAIL', 'MY_PASSWORD')
正如我所說的......您使用“會話”走在正確的軌道上,因為它將處理 cookies 並且您已經正確地復制了站點在調用時發送的有效負載。
我認為您可能缺少一些標頭...查看瀏覽器中完成的請求的“請求標頭”
其中最重要的一個是accept: application/json
,大多數時候需要正確的 JSON 通信。 如果這還不夠……開始將其他的 1 乘 1 添加,直到你得到你想要的……或者從所有這些開始並開始消除它們,直到你只剩下最低限度。 復制user-agent: ...
也可能是個好主意,以試圖隱藏您正在使用腳本。
在代碼中,您所要做的就是遵循以下幾行:
def login(email, password):
...
headers = {
"accept": "application/json",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
# ... eventually other headers
}
res = s.post('https://areariservata.subito.it/hera-api/login', json=payload, headers=headers)
# ... the rest of the code
return s # you initially forgot this one
附帶說明...我會使用臨時/一次性帳戶,直到您獲得令人滿意/有效的腳本,以避免因多次登錄嘗試而被“禁止”。 如果你的 IP 被禁止,通常重啟你的路由器應該改變你的公共 IP 所以它應該足夠了。
對於登錄response.status_code == 200
的確認應該足夠了......我嘗試隨機輸入得到了 401。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.