0

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 :(

Reiden
  • 61
  • 1
  • 1
  • 5
  • Does this answer your question? [Login to a website using python](https://stackoverflow.com/questions/31233421/login-to-a-website-using-python) – bertdida Aug 13 '20 at 09:16
  • whats the title when you're logged in? – Just for fun Aug 13 '20 at 10:28
  • Please format your code to be valid Python syntax – dh762 Aug 13 '20 at 14:12
  • @bertdida No, why would you flag my post as a duplicate question? It's nowhere near that one you linked. I used string correctly. – Reiden Aug 14 '20 at 02:23
  • @Poojan The title should be "Darkside RO - The Rise of Skywalker – Reiden Aug 14 '20 at 02:24
  • @dh762 Sorry I barely use stackoverflow, so I didn't know much about fomatting. sorry very much – Reiden Aug 14 '20 at 02:24
  • @Reiden I mean if you just look at your post you can see what's visually wrong. What's this line mean? `html = r.text soup = BeautifulSoup(html, "html.parser")` or this? `r = [s.post](https://s.post)(url, data=logdata)` it's obviously wrong. – Adam Smith Aug 14 '20 at 02:49

1 Answers1

1

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">
bertdida
  • 4,988
  • 2
  • 16
  • 22