Search code examples
pythonhttppython-requestsrequestpython-requests-html

Can't login using requests


How do i to a website using requests library, i watched a lot of tutorials but they seem to have a 302 POST request in their networks tab in inspector. I see a lot of GET requests in my tab when i login. A friend of mine said cookies but i am really a beginner i don't know how to login.

Also, i would like to know the range from which i can use this library or any helpful source of information from where i can begin learning this library.


import requests

r = requests.get("https://example.com")

I want to POST request, the same friend told me that i would require API access of that website to proceed further is it true?


Solution

  • Depending on the site that you are trying to log in with it may be necessary to log in via a chrome browser (selenium) and from there extract and save the cookies for further injection and use within the requests module.

    To extract cookies from selenium and save them to a json file use:

    cookies = driver.get_cookies()
    with open('file_you_want_to_save_the_cookies_to.json', 'w') as f:
      json.dump(cookies, f)
    

    To then use these cookies in the request module use:

    cookies = {
      'cookie_name' : 'cookie_value'
    }
    with requests.Session() as s:
      r = s.get(url, headers=headers, cookies=cookies)
    

    I could help further if you mention what site you are trying to do this on