Python 2.7 如何在Python中获取html文本字段的值?

Python 2.7 如何在Python中获取html文本字段的值?,python-2.7,Python 2.7,我有一个Python代码,当我输入127.0.0.1\dateform作为示例时,它显示了一个由Python创建的简单HTML页面,它只有一个文本框和一个提交按钮。现在我想得到文本框的值。我几乎用尽了一切办法,但什么也找不到。顺便说一下,我对Python非常陌生 请告诉我怎么可能 谢谢。您有两个选择: 首先是使用表格: <forms action="/path/to/function" method="post"> <textarea name="text_box"&g

我有一个Python代码,当我输入127.0.0.1\dateform作为示例时,它显示了一个由Python创建的简单HTML页面,它只有一个文本框和一个提交按钮。现在我想得到文本框的值。我几乎用尽了一切办法,但什么也找不到。顺便说一下,我对Python非常陌生

请告诉我怎么可能

谢谢。

您有两个选择:

首先是使用表格:

<forms action="/path/to/function" method="post">
    <textarea name="text_box">This is the value</textarea>
    <button type="submit">Submit</button>
</form>
第二种选择是使用JavaScript,但这可能不是您想要的

我希望您已经设置了cgi环境

假设你有一张表格

form.html

<form action="/cgi-bin/form.py" method="get" enctype="multipart/form-data">

Text input: <input maxlength="60" size="60" value="0" name="textinput"> 

<input value="Submit" type="submit">

</form>

文本输入:
form.py(位于cgi bin文件夹内)

导入cgi
form=cgi.FieldStorage()
#html表单输入是简单的键值对-
#在php中,它们是关联数组
#在ruby中,它们是散列
#在python字典中
#键是表单元素的名称(在我们的示例“textinput”中)
#值是用户输入
input_text=form.getfirst(“textinput”,“0”)#0是可选的-如果否
#输入的值会自动将其设置为“0”。等同于
#>>>x={2:“a”}
#>>>x.get(2,“不存在”)
#“a”
#>>>x.get(3,“不存在”)
#“不存在”
#看看这个-http://www.tutorialspoint.com/python/dictionary_get.htm
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
#打印“%s

%form”#如果需要,请取消对此行的注释 #看看表单散列是什么样子的 打印“%s

%”输入文本 打印“” 打印“”
据我所知,我无法使用路径,因为我不知道文件将在何处使用,不,我不使用Django。您使用的是什么web框架?在任何情况下,该框架都应该能够处理某种形式的
request
对象和URL。在代码中,我运行一个服务器,使用套接字,绑定并侦听。我试图用cgi获取值,但FieldStorage总是空的。
<form action="/cgi-bin/form.py" method="get" enctype="multipart/form-data">

Text input: <input maxlength="60" size="60" value="0" name="textinput"> 

<input value="Submit" type="submit">

</form>
import cgi   

form = cgi.FieldStorage()   
# html form inputs are simple key value pairs - 
#in php they are associative arrays
#in ruby they are hashes
# and in python dictionaries
# keys are form elements' name (in our example 'textinput')
# values are user inputs
input_text = form.getfirst("textinput", "0") # 0 is optional - if no 
#values entered automatically set it to "0". same as
#>>> x = {2:"a"}
#>>> x.get(2, "nonexistent")
#'a'
#>>> x.get(3, "nonexistent")
#'nonexistent'
#have a look up this -http://www.tutorialspoint.com/python/dictionary_get.htm
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<body>"
#print "<p>%s</p>" % form  #uncomment this line if you want 
#to see what does form hashes look like 
print "<p>%s</p>" % input_text
print "</body>"
print "</html>"