[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python Script to get Info from Site not working

Jimbo

3/10/2010 11:07:00 PM

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. [b]I want to use a Python script
to get information from a webpage. [/b]

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

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 ##############
[/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>[/code]
4 Answers

Gabriel Genellina

3/11/2010 1:38:00 AM

0

En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo <nilly16@yahoo.com> escribió:

> I found a semi Python & internet tutorial here if anyone else would
> like it http://www.upriss.org.uk/python/ses...
>
> 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

--
Gabriel Genellina

Jimbo

3/11/2010 2:26:00 AM

0

On Mar 11, 12:38 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo <nill...@yahoo.com> escribió:
>
> > I found a semi Python & internet tutorial here if anyone else would
> > like ithttp://www.upriss.org.uk/python/ses...
>
> > 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.

> 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
>
> --
> Gabriel Genellina

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.

Steve Holden

3/11/2010 12:37:00 PM

0

Jimbo wrote:
> On Mar 11, 12:38 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>> En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo <nill...@yahoo.com> escribió:
>>
>>> I found a semi Python & internet tutorial here if anyone else would
>>> like ithttp://www.upriss.org.uk/python/ses...
>>> 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.
>
>> 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
>>
>> --
>> Gabriel Genellina
>
> 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/librar...

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
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Gabriel Genellina

3/11/2010 6:28:00 PM

0

En Wed, 10 Mar 2010 23:26:05 -0300, Jimbo <nilly16@yahoo.com> escribió:

> On Mar 11, 12:38 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>> En Wed, 10 Mar 2010 20:06:41 -0300, Jimbo <nill...@yahoo.com> escribió:
>>
>> > I found a semi Python & internet tutorial here if anyone else would
>> > like ithttp://www.upriss.org.uk/python/ses...
>>
>> 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

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

--
Gabriel Genellina