Python Forum
this would be silly if it as not driving mr crazy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: this would be silly if it as not driving mr crazy (/thread-666.html)

Pages: 1 2


this would be silly if it as not driving mr crazy - Blue Dog - Oct-27-2016

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


RE: this would be silly if it as not driving mr crazy - Yoriz - Oct-27-2016

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.


RE: this would be silly if it as not driving mr crazy - snippsat - Oct-27-2016

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 



RE: this would be silly if it as not driving mr crazy - metulburr - Oct-27-2016

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?


RE: this would be silly if it as not driving mr crazy - Blue Dog - Oct-27-2016

I can use re  uests with 2.7, I can with 3.5. see photo:


[attachment=56]


RE: this would be silly if it as not driving mr crazy - metulburr - Oct-27-2016

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.


RE: this would be silly if it as not driving mr crazy - snippsat - Oct-27-2016

(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                                                                                         
λ 



RE: this would be silly if it as not driving mr crazy - Blue Dog - Oct-27-2016

thank you, did not know you have to install it for 2.7 and 3.5. It is done


RE: this would be silly if it as not driving mr crazy - metulburr - Oct-27-2016

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


RE: this would be silly if it as not driving mr crazy - Blue Dog - Oct-27-2016

It runs now, I get no error, but it does nothing. See photo:


[attachment=58]


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?