Jump to content


Photo

Chapter 2 - Starbuzz Code Problem


  • Please log in to reply
7 replies to this topic

#1 helenb

helenb

    New Member

  • Members
  • Pip
  • 7 posts
  • Gender:Male

Posted 12 July 2011 - 01:57 AM

I'm using Python 3.2.1 but get an error when running the very first bit of code from Chapter 2.

My code:
import urllib.request
page = urllib.request.urlopen("http://www.beans-r-u...iz/prices.html")
text = page.read().decode("utf8")
print(text)


The error message I get on run:

Traceback (most recent call last):
File "C:\Users\IQL Ltd\Documents\pricebean.py", line 2, in <module>
page = urllib.request.urlopen("http://www.beans-r-u...iz/prices.html")
File "C:\Python32\lib\urllib\request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "C:\Python32\lib\urllib\request.py", line 375, in open
response = meth(req, response)
File "C:\Python32\lib\urllib\request.py", line 487, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python32\lib\urllib\request.py", line 407, in error
result = self._call_chain(*args)
File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
result = func(*args)
File "C:\Python32\lib\urllib\request.py", line 560, in http_error_302
headers, fp)
urllib.error.HTTPError: HTTP Error 302: Found - Redirection to url '/PMPcL/prices.html' is not allowed


Any help?

#2 Sean Rogers

Sean Rogers

    New Member

  • Members
  • Pip
  • 2 posts

Posted 12 July 2011 - 02:32 PM

I am getting the essentially same result with python 3.2.1 freshly installed on two computers: my work Mac computer and Mac laptop (both OS X 10.6.5). The tracebacks in my cases refer to "3.2" instead of "32" in yours.

Using the python prompt to enter ' import urllib.request ' does not result in an error. A list is produced in response to 'dir(urllib)'.

#3 lpem

lpem

    New Member

  • Members
  • Pip
  • 1 posts

Posted 13 July 2011 - 08:32 AM

I'm having the same problem, can someone help us sort out the captain and 2.

#4 Sean Rogers

Sean Rogers

    New Member

  • Members
  • Pip
  • 2 posts

Posted 13 July 2011 - 01:26 PM

The book is written for Python 3.0.
The library has been re-configured in 3.2.1; so the code from the book is broken.
Maybe this should be noted in the errata.

My fix-around = Install Python 3.0, forget 3.2.1 for the meantime, and get on with learning to programme.

#5 helenb

helenb

    New Member

  • Members
  • Pip
  • 7 posts
  • Gender:Male

Posted 18 July 2011 - 12:47 AM

My fix-around = Install Python 3.0, forget 3.2.1 for the meantime, and get on with learning to programme.


Fair enough, it works with 3.0! Thanks.

#6 philberndt

philberndt

    New Member

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:Oslo, Norway

Posted 07 October 2011 - 03:03 AM

well in python 3.2.2 i get an error:

Traceback (most recent call last):
  File "/Users/philippberndt/04_starbuzz2.py", line 3, in <module>
    while price > 4.74:
TypeError: unorderable types: str() > float()

so by looking at the error type it seems that i need to convert price from string to float so i did the following:

import urllib.request
price_float = 99.99
while price_float > 4.74:
    page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
    text = page.read().decode("utf-8")
    where = text.find(">$")
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    price = text[start_of_price:end_of_price]
    price_float = float(price)
print ("buy")

and it works just fine

#7 philberndt

philberndt

    New Member

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:Oslo, Norway

Posted 07 October 2011 - 03:14 AM

I'm using Python 3.2.1 but get an error when running the very first bit of code from Chapter 2.

My code:
import urllib.request
page = urllib.request.urlopen("http://www.beans-r-u...iz/prices.html")
text = page.read().decode("utf8")
print(text)


The error message I get on run:

Traceback (most recent call last):
File "C:\Users\IQL Ltd\Documents\pricebean.py", line 2, in <module>
page = urllib.request.urlopen("http://www.beans-r-u...iz/prices.html")
File "C:\Python32\lib\urllib\request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "C:\Python32\lib\urllib\request.py", line 375, in open
response = meth(req, response)
File "C:\Python32\lib\urllib\request.py", line 487, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python32\lib\urllib\request.py", line 407, in error
result = self._call_chain(*args)
File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
result = func(*args)
File "C:\Python32\lib\urllib\request.py", line 560, in http_error_302
headers, fp)
urllib.error.HTTPError: HTTP Error 302: Found - Redirection to url '/PMPcL/prices.html' is not allowed


Any help?



the site is redirected to
http://beans.itcarlow.ie/prices.html
use that url in your code

#8 hmarro

hmarro

    New Member

  • Members
  • Pip
  • 5 posts

Posted 04 July 2012 - 06:34 PM

Try this code:

import urllib2
page = urllib2.urlopen("http://www.beans-r-u...iz/prices.html")
text = page.read().decode("utf8")
print(text)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users