Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have python 2.7 installed and I'm facing errors while trying to run scripts in Python. The GUI based Launchpad app Ground Control can't start and is facing bugs.

But even a simple program can't run:

from urllib import urlencode
from urllib2 import urlopen

pg = urlopen("http://www.beans-r-us.biz/proces.html")

text = pg.read().decode("utf8")
where = text.find(">$")
start_of_price = where + 2
end_og_price = start_og_price + 4
price = float(text[start_of_price:end_of_price])

print "The current price of coffee is:", price

The error log is as follows:

Traceback (most recent call last):
  File "androidscript.py", line 4, in <module>
    pg = urlopen("http://www.beans-r-us.biz/proces.html")
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 432, in error
    result = self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 619, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
anant@anant-Inspiron-N5010:~/Documents$

Someone please help me out with it.

share|improve this question
2  
We can only comment on your sample script. Obviously the URL you gave it does not exist (Error 404). Try with: http://beans.itcarlow.ie/prices.html –  Takkat Mar 4 '12 at 8:30
    
You have many typos. end_og_price, proces etc. –  user44549 Mar 4 '12 at 9:24
    
The link solves my problem. Thanks, and sorry for my stupidity. –  Anant Gupta Mar 4 '12 at 9:40

3 Answers 3

Are you absolutely certain that you spelled that address correctly? And that your internet connection is working? Because the error you get is HTTP 404, meaning the resource can't be found -- not some strange python internal error.

share|improve this answer
    
Sorry about the URL not working, I didn't know that. I took it from linuxjournal.com/article/10940?page=0,2 –  Anant Gupta Mar 4 '12 at 9:39

Try This Instead:

from urllib import urlencode
from urllib2 import urlopen

pg = urlopen("http://www.beans-r-us.biz/prices.html")

text = pg.read().decode("utf8") 
where = text.find('>$') 
start_of_price = where + 2 
end_of_price = start_of_price + 4 
price = float(text[start_of_price:end_of_price])

print "The current price of coffee is:", price
share|improve this answer

Here's one that works:

from urllib import urlencode
from urllib2 import urlopen
try:
    #pg = urlopen("http://www.beans-r-us.biz/proces.html")
    pg = urlopen("http://beans.itcarlow.ie/prices.html")
except:
    pg = False
#if pg!=False:    
if pg:    
    text = pg.read().decode("utf8")
    where = text.find(">$")
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    try:
        price = float(text[start_of_price:end_of_price])
    except:
        price = 'unknown.  Some error happened parsing the price...'
else:
    price = 'unknown.  Some error happened while getting the url...'
print "The current price of coffee is:", price

Slight improvement in that it handles the invalid URL a bit more gracefully...although not descriptively.

edit: change pg!=False to pg credit to Jo. He's right that it's cleaner. I thought it might make more sense to someone copy pasting code to see the false spelled out.

share|improve this answer
    
"if something != False:" is unnecessary. It's cleaner to use "if something:", which is the exact same thing to the interpreter. The float conversion should be in a try clause, though. It's highly likely it'll fail at some point. It should be handled properly. Good answer none the less. :) –  Jo-Erlend Schinstad Mar 4 '12 at 9:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.