填写在线表格(Fill out online form)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

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

解决方案

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

本文地址:IT屋 » Fill out online form

问 题

我正在尝试使用用户名和密码填写位于 https://idp.ncedcloud.org/idp/AuthnEngine#/authn 的表单。我想知道是否成功通过。我试过它ith python2,但我无法让它工作。



 #!/ 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)
打印(内容)


错误:

它打印同一网页的内容,我希望它打印新的网页内容。
所需行为:
python3解决方案进入下一个网页,或者为什么我的代码无效


解决方案

使用请求图书馆。我建议使用会话,以便为以下任何请求保存您的登录信息:



 导入请求

url =“http://example.com”

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

s = requests.Session()

#如果它支持基本身份验证你可以只做$ b请求前$ b#s.auth(用户名,密码)

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

print(req.text)


然后应该可以使用 s 会话对象


本文地址:IT屋 » 填写在线表格

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起