简体   繁体   中英

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.

例

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. 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. 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. 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.

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 You can go through the documentation to easily achieve what you are trying.

I think you should start with Django to be honest. 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.

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

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.

Codeacademy has some good and free tutorials btw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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