How to “log in” to a website using Python's Requests module?

前端 未结 6 1744
别那么骄傲
别那么骄傲 2020-11-22 02:48

I am trying to post a request to log in to a website using the Requests module in Python but its not really working. I\'m new to this...so I can\'t figure out if I should ma

6条回答
  •  無奈伤痛
    2020-11-22 03:06

    Let me try to make it simple, suppose URL of the site is http://example.com/ and let's suppose you need to sign up by filling username and password, so we go to the login page say http://example.com/login.php now and view it's source code and search for the action URL it will be in form tag something like

     

    now take userinfo.php to make absolute URL which will be 'http://example.com/userinfo.php', now run a simple python script

    import requests
    url = 'http://example.com/userinfo.php'
    values = {'username': 'user',
              'password': 'pass'}
    
    r = requests.post(url, data=values)
    print r.content
    

    I Hope that this helps someone somewhere someday.

提交回复
热议问题