O'Reilly Forums: Chapter 2 - Starbuzz Code Problem - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 2 - Starbuzz Code Problem

#1 User is offline   helenb 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 12-July 11
  • 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-us.biz/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-us.biz/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?
0

#2 User is offline   Sean Rogers 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 12-July 11

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)'.
0

#3 User is offline   lpem 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 13-July 11

Posted 13 July 2011 - 08:32 AM

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

#4 User is offline   Sean Rogers 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 12-July 11

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

#5 User is offline   helenb 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 12-July 11
  • Gender:Male

Posted 18 July 2011 - 12:47 AM

View PostSean Rogers, on 13 July 2011 - 01:26 PM, said:


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

#6 User is offline   philberndt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 07-October 11
  • 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
0

#7 User is offline   philberndt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 07-October 11
  • Gender:Male
  • Location:Oslo, Norway

Posted 07 October 2011 - 03:14 AM

View Posthelenb, on 12 July 2011 - 01:57 AM, said:

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-us.biz/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-us.biz/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
0

#8 User is offline   hmarro 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 04-July 12

Posted 04 July 2012 - 06:34 PM

Try this code:

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users