简体   繁体   中英

How to login to this website with Python

I am trying to login to a website with requests library in Python. I tried different codes with no luck. This is my code:

url = 'https://www.damejidlo.cz/en/profile/prihlaseni'
login_data = {'email': 'test',
              'password': 'password',
              'do': 'loginForm-submit'}
session = requests.session()
session.headers.update({'do': 'loginForm-submit'})
result = session.post(url, data=login_data)
if "test@lolco.net" in result.content:
    print("example.com:", "logged in")
elif "Sign up" in result.content:
    print("example.com:", "not logged in")

How to make it work? You can use user data to test login. I will delete that account afterwards. Thanks!

If I run this code, I get example.com: not logged in . I wanna be logged in :D

It is very simple, you just need to add a user-agent:

In [7]: import requests

In [8]: from bs4 import BeautifulSoup

In [9]: login = "https://www.damejidlo.cz/en/profile/prihlaseni"

In [10]: data = {"email": "xxxxxxxxx@gmail.com",
   ....:         "password": "xxxxxxxx",
   ....:         "do": "loginForm-submit"}

In [11]: h = {
   ....:     "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"}

In [12]: with requests.Session() as s:
   ....:         r = s.post(login, data=data, headers=h)
   ....:         soup = BeautifulSoup(r.content,"lxml")
   ....:         print(soup.select_one("#snippet--user-bar a.username.user-bar__links-item").text)
   ....:     

xxxxxxxxx@gmail.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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