Python Forum
this would be silly if it as not driving mr crazy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
this would be silly if it as not driving mr crazy
#1
I am using 2.7.3
import urllib
htmlfile = urllib.urlopen("http://www.google.com")
htmltext = htmlfile.read()
this is the error I am geting
Error:
Traceback (most recent call last):   File "C:/Users/renny and kite/Documents/Hacking with python/web scraping tut/getting_page_html", line 1, in <module>     htmlfile = urllib.urlopen("http://www.google.com") NameError: name 'urllib' is not defined
I went through 15 learn python videos, this never came up in the course. I would think people that teach this stuff would put error in the course
so when you get one you know how to fix it. Code that run fine don't do you any good when you tyring fine errors

This is so simple 4 line s of code and it does not work,
why?
thank you
Reply
#2
Strange I would expect that error if the import line was missing.
Edit: The error says it is on line one so the import is missing, the code you pasted is not the code you are using.
Reply
#3
As mention bye Yoriz the code you post work.
So are not running that code to get error message you post.

Drop urrlib and always use Requests.
import requests

htmlfile = requests.get('http://www.google.com')
htmltext = htmlfile.text
Test:
>>> htmlfile.status_code
200
>>> htmlfile.encoding
'ISO-8859-1'
>>> htmlfile.headers
{'content-length': '4622', 'x-xss-protection': '1; mode=block', 'content-encoding': 'gzip',...ect
htmltext # Source code 
Reply
#4
Quote:
import urllib
Are you sure your import line is like this? Its not from urllib etc. or import urllib as etc. Are you sure it exists at all on the code you are running?
Recommended Tutorials:
Reply
#5
I can use re  uests with 2.7, I can with 3.5. see photo:


   
Reply
#6
requests is a 3rd party library....you need to install that.

But urllib is standard. That should work regardless. How did you install python? More info regarding that.
Recommended Tutorials:
Reply
#7
(Oct-27-2016, 03:09 PM)Blue Dog Wrote: I can use re  uests with 2.7, I can with 3.5. see photo:
You have to install it,
and upgrade to Python 2.7.11(then it comes with pip pre-installed).
It goes like this,first upgrade pip to 8.1.2
Then install Request.
Eg.
C:\                                                                                                 
λ cd python27/scripts                                                                               
                                                                                                    
C:\Python27\Scripts                                                                                 
λ pip install --upgrade pip                                                                         
Requirement already up-to-date: pip in c:\python27\lib\site-packages                                
                                                                                                    
C:\Python27\Scripts                                                                                 
λ pip -V                                                                                         
pip 8.1.2 from c:\python27\lib\site-packages (python 2.7)                                           
                                                                                           
C:\Python27\Scripts                                                                                            
λ pip install requests                                                                              
Requirement already satisfied (use --upgrade to upgrade): requests in c:\python27\lib\site-packages 
                                                                                                    
C:\Python27                                                                                         
λ 
Reply
#8
thank you, did not know you have to install it for 2.7 and 3.5. It is done
Reply
#9
(Oct-27-2016, 05:43 PM)Blue Dog Wrote: did not know you have to install it for 2.7 and 3.5. It is done
Each one is a different install. If you had python3.5 32 bit, python3.5 64 bit, python 2.7 32 bit, python2.7 64 bit, python3.4 etc., you would have to install it for all 5 python installs.

I think you should still fix you import urllib problem. Either you forgot some code, or you could have a faulty install. Such as if you compile python yourself and forgot something.
Recommended Tutorials:
Reply
#10
It runs now, I get no error, but it does nothing. See photo:


   


The script just like this,but using urllib displayed the html of the site.
This just stop and does nothing.
what am I doing wrong?
Thank you

"I think you should still fix you import urllib problem."
Should I reinstall evrything?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A crazy project involving webpage interaction apsaditya 7 5,057 May-20-2018, 05:39 AM
Last Post: apsaditya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020