简体   繁体   English

如何使用 python 请求登录网站?

[英]How can i login to website with python requests?

I would like to login to a website using python requests, the site in question is: https://www.subito.it/我想使用 python 请求登录网站,相关网站是: https://www.subito.it/

Also I would like to know how to confirm that the program is able to login.另外我想知道如何确认程序能够登录。

This is my code:这是我的代码:

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

As I was saying... you were on the right track by using "session" as it will take care of the cookies and you've correctly replicated the payload that the site is sending when it makes the call.正如我所说的......您使用“会话”走在正确的轨道上,因为它将处理 cookies 并且您已经正确地复制了站点在调用时发送的有效负载。
I think that you might missing be some headers... take a look at the "Request Headers" on the request done in the browser我认为您可能缺少一些标头...查看浏览器中完成的请求的“请求标头” “”

One of the most important ones would be accept: application/json , most of the times is needed to have a correct JSON communication.其中最重要的一个是accept: application/json ,大多数时候需要正确的 JSON 通信。 If that's not enough... start adding the other ones 1 by 1 until you get what you want... or start with all of them and start eliminating them until you're left with the bare minimum.如果这还不够……开始将其他的 1 乘 1 添加,直到你得到你想要的……或者从所有这些开始并开始消除它们,直到你只剩下最低限度。 It might also be a good idea to copy the user-agent: ... in order to try to hide that you are using a script.复制user-agent: ...也可能是个好主意,以试图隐藏您正在使用脚本。

In the code all you'd have to do is something along these lines:在代码中,您所要做的就是遵循以下几行:

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

On a side note... I'd use a temporary/disposable account, until you get a satisfying/working script, in order to avoid getting a "ban" for too many login attempts.附带说明...我会使用临时/一次性帐户,直到您获得令人满意/有效的脚本,以避免因多次登录尝试而被“禁止”。 In case you get your IP banned, usually a restart of your router should change your public IP so it should be enough.如果你的 IP 被禁止,通常重启你的路由器应该改变你的公共 IP 所以它应该足够了。

For the confirmation of the login response.status_code == 200 should be enough... my attempt with random input got me a 401.对于登录response.status_code == 200的确认应该足够了......我尝试随机输入得到了 401。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM