0

I would like to simply log in to a webpage (the one of my university) using requests or any other module that you guys recommend. The problem is, that on the internet you only find tutorials on how to do this on some 'self-build' websites with no cookies or anything else. Sometimes the tricky part is to pass a middlewaretoken or something, but it seems like this website does not have one.

I'm really not into cookies and stuff. So I don't know if I need to save them (and which), which ones are important and how to handle them in general.

The following screenshots show the cookies and headers that belong to the login form. It seems like there is some sort of redirect happening after you logged in to double check the session cookie or something.

I hope it's fine to post that many pictures, but I think that all of these are important to solve the problem.

The question asked now is: How can I log into a website that uses cookies to handle the session. It would be nice if you could help me with my specific problem, but I'm also happy if you just help me with the question and handling cookies in general.

I am just able to upload 2 pictures: testsession = 72087 is the redirect. It uses the 'GET' method and the response cookie (MoodleSession) is what you end up with. So it seems to get passed or something.

Ask me if you need more details or just check out the page I am talking about.

I'm glad for all the help I can get. Cookies1 Headers1

S ProaBusi
  • 59
  • 5

2 Answers2

0

You might want to try using selenium package for automating navigation to websites. Once you login with selenium you can continue working in the same session.

I took this code from an answer to this StackOverflow question:

driver = webdriver.Chrome(...)

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

username.send_keys("YourUsername")
password.send_keys("Pa55worD")

driver.find_element_by_name("submit").click()
crow3487
  • 135
  • 1
  • 10
0

You have to read API doc of requests , there are a parameter allow_redirects. Set to False.

import requests

url = "https://moodle.uni-due.de/login/index.php"

headers = {
    "Origin": "https://moodle.uni-due.de",
    "Referer": "https://moodle.uni-due.de/login/index.php",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"
}

data = {
    "username":"j******n",
    "password":"j*******Y",
    "anchor":""

}


with requests.Session() as se:
    res = se.post(url , headers = headers , data = data , allow_redirects = False)
    print(res.cookies)

Output

<RequestsCookieJar[<Cookie MoodleSession=7ip39c4u******j7pvk4tiola6 for moodle.uni-due.de/>]>
KC.
  • 2,981
  • 2
  • 12
  • 22