Python Script to get Info from Site not working

J

Jimbo

Hello

I am asking what is probably a very easy thing to do but I cannot find
a tutorial on how to do it anywhere. I want to use a Python script
to get information from a webpage.


I found a semi Python & internet tutorial here if anyone else would
like it http://www.upriss.org.uk/python/session6.html

I would like to create a python script that gets a value from a set of
radio buttons on a HTML page & tell me which one is selected.

I have 3 radio buttons: Tea, Coffee, Hot Chocolate & a Submit button

My script is meant to find which radio button is selected & tell me
that. But it just keeps saying "No Drink Selected!"

Can you help me figure out how to fix this. FYI: My Python script & my
HTML page are both saved in the same directory. And I made the webpage
using Notepad++, I open the webpage in Interent Explorer & I made the
Python Script in Idle.

Python Script:
Code:
# Practice 9 CGI

######### don't change the following three lines: ###########
import cgi
print("Content-Type: text/html\n")
form = cgi.FieldStorage()

## add a form.getvalue for each of the names in your form: ##
drink = form.getvalue("drink")

########## start of HTML code ###########
print("""
<html>
<head> <title>What would you like to drink</title> </head>
<body>
<h4>Your drink: </h4><p>
""")
############ end of HTML code #############

if drink == "tea":
    print("You requested tea.")
elif drink == "coffee":
    print("You requested coffee.")
elif drink == "hot chocolate":
    print ("You requested hot chocolate.")
else:
    print ("You need to select a drink!")

########### start of HTML code ###########
print ("""
<p>Thank you for your visit. Please come again. <p>
</body></html>
""")
############# end of HTML code ##############

HTML Code:
Code:
<html>
<head>


</head>

<body>

<form action="practice9.py" method="post">

<input type="radio" name="drink" value="tea" checked > Tea <br>
<input type="radio" name="drink" value="coffee" > Coffee <br>
<input type="radio" name="drink" value="hot chocolate" > Hot Chocolate
<p>

<input type="submit" value="Place order">
</form>


</body>
</html>
 
G

Gabriel Genellina

I found a semi Python & internet tutorial here if anyone else would
like it http://www.upriss.org.uk/python/session6.html

My script is meant to find which radio button is selected & tell me
that. But it just keeps saying "No Drink Selected!"

Are you sure? From the code you posted, the message should read "You need
to select a drink!", not that one.
if drink == "tea":
print("You requested tea.")
elif drink == "coffee":
print("You requested coffee.")
elif drink == "hot chocolate":
print ("You requested hot chocolate.")
else:
print ("You need to select a drink!")

Replace the last line with something like this, to see what you are
getting exactly:

print("Sorry, no %r (%r) available." % (
drink,
cgi.escape(repr(type(drink)))))

BTW, which Python version are you using? The tutorial you linked to is
aimed at Python 2.x, but your print syntax suggests you're using Python 3.x
 
J

Jimbo

Are you sure? From the code you posted, the message should read "You need  
to select a drink!", not that one.
Replace the last line with something like this, to see what you are  
getting exactly:

        print("Sorry, no %r (%r) available." % (
           drink,
           cgi.escape(repr(type(drink)))))

BTW, which Python version are you using? The tutorial you linked to is  
aimed at Python 2.x, but your print syntax suggests you're using Python 3..x

Yeah that("You need to select a drink!") was the string I was getting
but I should be getting hot chocolate or something else(which ever one
I have selected)

I am using 3.x, do you think I should be using something else? Also
the your code I put in does the some thing, just now it says "Sorry no
none drink available" all the time.
 
S

Steve Holden

Jimbo said:
Yeah that("You need to select a drink!") was the string I was getting
but I should be getting hot chocolate or something else(which ever one
I have selected)

I am using 3.x, do you think I should be using something else? Also
the your code I put in does the some thing, just now it says "Sorry no
none drink available" all the time.

It may not seem like it, but you *are* making progress here (of a sort).
From the information you have given us so far it appears that your call
to form.getvalue() is returning None (which is of type <type 'NoneType'>
- your browser is probably not displaying this because it sees it as a
bogus HTML tag.

Perhaps you haven't yet realised the importance of exact copy-and-paste
here. What I'd like you to do is change the line

drink = form.getvalue("drink")

to

drink = form.getfirst("drink", "No drink selected")

then have you browser display the HTML source of the response page your
program is returning and paste that into a reply to this mail.

You might also want to take a look at

http://docs.python.org/3.1/library/cgi.html

for further information about the Python library your are trying to use.
In particular I'd recommend heeding its advice about use of the cgitb
module.

regards
Steve
 
G

Gabriel Genellina

I am using 3.x, do you think I should be using something else?

Yes, you should use the same version as the tutorial you're following.
Learning how to program, CGI, HTML and Python at the same time is hard
enough to complicate it with incompatible version differences. I suggest
Python 2.6.4
Also
the your code I put in does the some thing, just now it says "Sorry no
none drink available" all the time.

As a general rule, always directly copy and paste the error messages you
get and the source code you execute. Do not retype or paraphrase them.
That's important for the rest of us to be able to help you - now and in
the future.

In this case, the message should read "Sorry, no None (<type 'NoneType'>)
available." (None, not none, is a built-in special object in Python). So
we know that drink is None instead of one of the expected values, and we
could start investigating why. But there is no point in digging further -
instead, downgrade to Python 2.6.4 and try again.
This might be a server issue, unrelated to your script. BTW, you didn't
mention the server software.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top