2018年7月27日 星期五

[ Python 常見問題 ] requests - How to “log in” to a website?

Source From Here
Question
I am trying to post a request to log in to a website using the Requests module in Python but its not really working.

How-To
If the information you want is on the page you are directed to immediately after login, below is the simple code:
  1. payload = {'inUserName''USERNAME/EMAIL''inUserPass''PASSWORD'}  
  2. url = 'http://www.locationary.com/home/index2.jsp'  
  3. requests.post(url, data=payload)  
Assuming your login attempt was successful, you can simply use the session instance to make further requests to the site. The cookie that identifies you will be used to authorize the requests:
  1. import requests  
  2.   
  3. # Fill in your details here to be posted to the login form.  
  4. payload = {  
  5.     'inUserName''username',  
  6.     'inUserPass''password'  
  7. }  
  8.   
  9. # Use 'with' to ensure the session context is closed after use.  
  10. with requests.Session() as s:  
  11.     p = s.post('LOGIN_URL', data=payload)  
  12.     # print the html returned or something more intelligent to see if it's a successful login page.  
  13.     print p.text  
  14.   
  15.     # An authorised request.  
  16.     r = s.get('A protected web page url')  
  17.     print r.text  
  18.         # etc...  

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...