Jump to content


Photo

Chapter 2 Code


  • Please log in to reply
14 replies to this topic

#1 SageJ

SageJ

    New Member

  • Members
  • Pip
  • 3 posts

Posted 15 March 2014 - 08:25 PM

I've been all over the forums on here and have seem many issues with the chapter 2 code. I have tried all of the different urls and none of them work for me. I'm not able to go past page 40 because I can't import the raw html data. Any suggestions?

CODE
import urllib.request

page=urllib.request.urlopen("http://beans-r-us.ap...om/prices.html")
text=page.read().decoder("utf8")

print (text)

ERROR
import urllib.request
ImportError: No module named request

#2 Stroodle88

Stroodle88

    New Member

  • Members
  • Pip
  • 8 posts
  • Gender:Male
  • Location:Columbia River Gorge, USA
  • Interests:• Guitar
    • mountain biking
    • learning Python
    • digital trading-card games
    • karaoke

Posted 15 March 2014 - 09:26 PM

SageJ:

The error you're getting implies not that the URL is incorrect, but that whatever IDE you're using to write your Python code isn't recognizing the request method. I've just copied and pasted the code you pasted above into two different IDEs. In one, I was able to reproduce the error you posted above. In the other, the import function works. The error code you're seeing implies the IDE you're using can't find or doesn't recognize the request method.

You should try a version of IDLE to test your code. I'm using IDLE 3.3.3 without a problem using the URL you posted above. Let me know if you have any questions about what I've written.

#3 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 306 posts

Posted 15 March 2014 - 11:54 PM

Stroodle88 is correct - this code works fine (after changing "decoder" to "decode") when executed in IDLE, as shown in the book.

--Paul.

#4 SageJ

SageJ

    New Member

  • Members
  • Pip
  • 3 posts

Posted 16 March 2014 - 09:12 AM

Thanks for the help. Now that I just wiped my cpu for a clean dev environment with only IDLE installed, the code works fine. Now to find an additional product key code for windows 8 on the cheap for a segregated partition for all my "messy" installs and explorations. Any suggestions as to how to handle this? It seems like a fairly sensible thing to keep a sterile dev. environment.

#5 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 306 posts

Posted 17 March 2014 - 12:31 PM

I don't know anything about Windows 8 keys, and such. But, if you want to keep your development stuff away from your regular stuff, I'd suggest installing VirtualBox from Oracle on your computer (free download, and free to use). With it, you can create virtual PC's onto which you can install most anything. Once you have VirtualBox up-and-running, I'd suggest installing the latest version of Ubuntu Linux (free download, and free to use). Total cost to you: nothing (other than your time and the downloads, of course). You can then experiment with Python on Ubuntu to your heart's content - and, of course, Python 3 and IDLE3 come pre-installed. :-)

Hope this suggestion helps.... and glad to see your problem is solved.

--Paul.

#6 bmike1

bmike1

    New Member

  • Members
  • Pip
  • 5 posts

Posted 14 April 2015 - 01:46 PM

got a problem: I was getting the same error as OP but I was using the BASH shell as the IDE. When I read in this thread that was the problem I installed idle with apt-get but still I get an error:

 

 
Traceback (most recent call last):
  File "/home/bmike1/Documents/Education/programming.training/Python/2-40starbuzz.py", line 1, in <module>
    import urllib.request
ImportError: No module named request
>>> 
 
what am i doing wrong?
 
---
my code
---
 
import urllib.request
 
page = urllib.request.urlopen("http://beans-r-us.ap...m/prices.html")
text = page.read().decode("utf8")
 
print(text)
 

Edited by bmike1, 14 April 2015 - 01:48 PM.


#7 bmike1

bmike1

    New Member

  • Members
  • Pip
  • 5 posts

Posted 14 April 2015 - 06:45 PM

my idle version is 2.7.6



#8 bmike1

bmike1

    New Member

  • Members
  • Pip
  • 5 posts

Posted 15 April 2015 - 09:49 AM

after a little more research I discovered my IDLE version might be at fault so I installed idle 3.4.0 and now everything works as it is supposed to.


Edited by bmike1, 15 April 2015 - 09:50 AM.


#9 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 19 April 2015 - 11:04 AM

Hi bmike1,
 
There is no problem with IDLE or Python here.
 
for Python 2.x it is

import urllib2

page = urllib2.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)

But for Python 3.X it is

import urllib.request

page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)

You can also avoid frequent usage of lengthy import statements by using as keyword.

import urllib.request as ur

page = ur.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)


Edited by #TM#, 19 April 2015 - 11:16 AM.


#10 dsf54

dsf54

    New Member

  • Members
  • Pip
  • 1 posts

Posted 24 April 2015 - 11:38 PM

import urllib.request

page = urllib.request.urlopen("http://www.beans-r-u...-loyalty.html")
text = page.read().decode("utf8")

price = text[234:230]
print (price)
 

I get it to run, put it doesn't come up with a price, it is just a blank spot??

 

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>

>>>

 

I have a Mac Lion. Again any ideas?



#11 bmike1

bmike1

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 April 2015 - 09:14 AM

Thank you TM. The 'as' keyword will be most beneficial!

 

Hi bmike1,
 
There is no problem with IDLE or Python here.
 
for Python 2.x it is

import urllib2

page = urllib2.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)

But for Python 3.X it is

import urllib.request

page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)

You can also avoid frequent usage of lengthy import statements by using as keyword.

import urllib.request as ur

page = ur.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
 
print(text)



#12 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 04 May 2015 - 06:17 AM

Thank you TM. The 'as' keyword will be most beneficial!

You are most welcome. Great to know it helps.smile.png



#13 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 04 May 2015 - 06:26 AM

import urllib.request

page = urllib.request.urlopen("http://www.beans-r-u...-loyalty.html")
text = page.read().decode("utf8")

price = text[234:230]
print (price)
 

I get it to run, put it doesn't come up with a price, it is just a blank spot??

 

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>

>>>

 

I have a Mac Lion. Again any ideas?

Hi dsf54, please paste code or tracebacks by using <> button on the editor mellow.png

 

In your code, the price position is not correct it is is 251 to 255 (obviously not including 255).

 

May be the book is using the same old position as your code but the site has updated since then tongue.png .

 

That's why it is better to search for the sign ">$" in the site and print accordingly.

import urllib.request

page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")
text = page.read().decode("utf8")

price = text[251:255] # Here lies the problem
print (price)

cool.png


Edited by #TM#, 04 May 2015 - 06:28 AM.


#14 Damien Norris

Damien Norris

    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 July 2015 - 12:17 PM

Hi,

 

Trying to go through Ch2 and no matter which version of the program I use I keep getting this:

Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 922, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 857, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 834, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 512, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 503, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\Programs\Coffee Biiz.py", line 7, in <module>
    page = urllib.request.urlopen("http://beans.itcarlow.ie/prices.html")
  File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1210, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond>
>>> 

Anyone any ideas?

 

Thanks



#15 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 12 July 2015 - 04:54 AM

Hi,

 

Trying to go through Ch2 and no matter which version of the program I use I keep getting this:

Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1182, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1126, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 922, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 857, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 834, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 512, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 503, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\Programs\Coffee Biiz.py", line 7, in <module>
    page = urllib.request.urlopen("http://beans.itcarlow.ie/prices.html")
  File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 463, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 481, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 441, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1210, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1184, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond>
>>> 

Anyone any ideas?

 

Thanks

Hi Damien, Check that you are connected to internet or not.

 

If you are connected; then check your proxy settings in IE or Chrome. If you are on windows (which you are on; i think from your traceback) and using LAN or Wi-Fi; it is usually "automatically detect my proxy settings" something like that.

 

In chrome, You can go to "chrome://settings/"; then click "show advance settings" and there is an option "change my proxy settings" and then change your LAN settings accordingly. :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users