Jump to content
Chapter 2 Code
12 replies to this topic
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?
ImportError: No module named request
Posted 15 March 2014 - 09:26 PM
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.
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.
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.
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.
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>
ImportError: No module named request
what am i doing wrong?
page = urllib.request.urlopen("http://beans-r-us.ap...m/prices.html")
text = page.read().decode("utf8")
Edited by bmike1, 14 April 2015 - 01:48 PM.
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.
Posted 19 April 2015 - 11:04 AM
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.
Posted 24 April 2015 - 11:38 PM
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)
I have a Mac Lion. Again any ideas?
Posted 27 April 2015 - 09:14 AM
Thank you TM. The 'as' keyword will be most beneficial!
Posted 04 May 2015 - 06:17 AM
You are most welcome. Great to know it helps.
Posted 04 May 2015 - 06:26 AM
Hi dsf54, please paste code or tracebacks by using <> button on the editor
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 .
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)
Edited by #TM#, 04 May 2015 - 06:28 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users