简体   繁体   English

使用Python从HTML的textarea获取数据

[英]Get data from textarea in HTML using Python

I am trying to print out to Python what the user types inside the textarea element in the HTML page. 我正在尝试向Python打印出用户在HTML页面的textarea元素内键入的内容。

例

If I type "hello world" into the textarea field and click the button 'Add comment', I would like to see it print out to the Python console. 如果我在textarea字段中输入“ hello world”并单击“添加评论”按钮,我希望看到它打印到Python控制台。 Can anyone help me with this? 谁能帮我这个?

a = ""

a += '<html> \r\n'
a += '<head> \r\n'
a += '</head> \r\n'
a += '<body> \r\n'
a += '<form method="post"> \r\n'
a +=  '<textarea name="comment101" style="width:300px; height: 70px; maxlength="300"; display:none;"> \r\n'
a +=  '</textarea></br> \r\n'
a +=  '<input type="submit" name="comment101" value="Add comment" /> \r\n'
a +=  '</form> \r\n'
a += '</body> \r\n'
a += '</html> \r\n'

b = open("test.html", "wb")
b.write(a)
b.close()

So looking from your conversion in the comments you seem to be a bit confused as how this works. 因此,从您的转换意见中看,您似乎有点困惑,因为它是如何工作的。 You cannot use python natively to listen to a button click of an html file and then retrieve this information. 您不能在本地使用python来侦听html文件的按钮单击,然后检索此信息。 Your going to need a framework in between to help you do this. 您将需要一个介于两者之间的框架来帮助您做到这一点。 I have listed some of the following below. 我在下面列出了以下一些内容。

Flask - This is my personal favorite, its light weight and easy to understand. 烧瓶 -这是我个人的最爱,它的重量轻且易于理解。 As it says on their website this is not a full blown framework its a microframework. 就像在他们的网站上说的那样,这不是一个完善的框架,而是一个微框架。 Easy to use and understand, a great place to start learning about frameworks. 易于使用和理解,是开始学习框架的好地方。

Django - Much more advanced a quite a bit more complicated to start learning. Django-更高级,开始学习时要复杂得多。 This framework has tons of features and its great in many cases. 该框架具有许多功能,并且在许多情况下具有出色的功能。 Its going to be a bit harder for someone new to frameworks to understand Django. 对于刚接触框架的人来说,理解Django会有些困难。

Both of these are going to require quite a bit of reading but are well worth it and will be a great way to advance your programming skills. 这两个都需要大量的阅读,但是非常值得,这将是提高您的编程技能的好方法。

You can also use CherryPy, which is a minimalist python framework which can help you achieve connecting your html code with native python code. 您还可以使用CherryPy,这是一个极简的python框架,可以帮助您实现html代码与本机python代码的连接。 CherryPy You can go through the documentation to easily achieve what you are trying. CherryPy您可以阅读文档以轻松实现所要尝试的内容。

I think you should start with Django to be honest. 老实说,我认为您应该从Django开始。 They have an excellent tutorial to get you up and running. 他们有一个出色的教程 ,可以帮助您入门和运行。 It is also feature complete as a framework. 它还具有完整的框架功能。 When you get more comfortable with Python and Django, I would move to frameworks like Flask that are excellent but require you to assemble the pieces. 当您对Python和Django更加满意时,我将转到Flask之类的框架,这些框架非常出色,但需要您组装它们。

But you will likely need to learn more about virtualenv , which is made easier with the virtualenvwrapper , and pip before you get started on that adventure 但是,你可能会需要更多地了解virtualenv中 ,它是由易后难virtualenvwrapperPIP你开始对之前的冒险

I would be happy to answer any additional questions you may have... Python and it's associated frameworks and tools are such a joy to work with. 我很乐意回答您可能还有的其他问题... Python及其相关的框架和工具非常适合您使用。

Codeacademy has some good and free tutorials btw Codeacademy有一些不错的免费教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM