Why cant I login into a website using request module

Tags: , , , ,



So I needed to login to a website as I need to do an action that requires logging in first. Here’s my code:

import requests from bs4 import BeautifulSoup

logdata = {'username': 'xxx', 'password': 'xxx'}
url = 'https://darkside-ro.com/?module=account&action=login&return_url='

with requests.Session() as s: 
    r = [s.post](https://s.post)(url, data=logdata)

html = r.text soup = BeautifulSoup(html, "html.parser") 
print(soup.title.get_text())

it gives me the title of when you’re not logged in :(

Answer

I’m not sure why did I flagged this as duplicate, sorry.


Okay, so I created a dummy account and tried logging in – I noticed that when I submit the form, the following data are sent to https://darkside-ro.com/?module=account&action=login&return_url=.

enter image description here

So to fix your issue, you have to include a server in your logdata dictionary.

import requests
from bs4 import BeautifulSoup

logdata = {
  'username': 'abc123456',
  'password': 'abc123456',
  'server': 'Darkside RO'
}

url = 'https://darkside-ro.com/?module=account&action=login&return_url='
with requests.Session() as s: 
    r = s.post(url, data=logdata)

html = r.text
soup = BeautifulSoup(html, 'html.parser') 
print(soup.title.get_text())

Running the code above will print

Darkside RO - The Rise of Skywalker

PS: When you do this things again, it would be a good idea to check for hidden inputs in the form by inspecting the elements. On the site above, it has

<input type="hidden" name="server" value="Darkside RO">


Source: stackoverflow