Fill out online form

2019-09-24 09:41发布

问题:

I'm trying to fill out the form located at https://idp.ncedcloud.org/idp/AuthnEngine#/authn with a username and password. I want to know if went through successfully or not. I tried it ith python2, but I couldn't get it to work.

#!/usr/bin/env python
import urllib
import urllib2

name =  "username"
name2 = "password"
data = { 
        "description" : name, 
        "ember501": name2
       }   

encoded_data = urllib.urlencode(data)
content = urllib2.urlopen("https://idp.ncedcloud.org/idp/AuthnEngine#/authn",
        encoded_data) 
print(content)

error:
It prints the content of the same webpage, and I want it to print the new webpage content. desired behavior: python3 solution that goes to the next webpage, or why my code isn't working

回答1:

Basic example of posting data to a webpage like this using requests library. I would suggest using session, so that your login info is saved for any following requests:

import requests

url = "http://example.com"

name =  "username"
name2 = "password"
data = { 
    "description" : name, 
    "ember501": name2
}   

s = requests.Session()

# If it supports basic auth you could just do 
# s.auth(username, password) here before a request 

req = s.post(url, data=data)

print(req.text)

Should then be able to do following requests with the s session object