AttributeError: module 'urllib' has no attribute 'urlopen' in Python
Hello you guys, I am a newbie in Python and I'm also studying more about Python.
I have some lines of code, I want to get all html from the some website to detect some data. I try code as below:
import urllib
stream = urllib.urlopen("https://www.facebook.com")
content = stream.read()
print(content)
But I get an exception AttributeError: module 'urllib' has no attribute 'urlopen' when I run code above.
Traceback (most recent call last):
File "main.py", line 3, in <module>
stream = urllib.urlopen("https://www.facebook.com")
AttributeError: module 'urllib' has no attribute 'urlopen'
And I am using python 3.8.2
Anyone can explain it to me? How can I solve it?
Thanks for any suggestions.
-
J1
Jair Rojas Garcia Oct 03 2021
Your code only work for version 2.x.
An with python 3.x you can change
import urllib
toimport urllib.request as url
. See example below:import urllib.request as url stream = url.urlopen("https://www.facebook.com") content = stream.read() print(content)
And it worked for you.
-
T0
Truong Thai Oct 03 2021
Below is solution for Python 3.x
from urllib.request import openlink url = 'http://www.python.org' file = openlink(url) response = file.read() print(response)
It work well for me!
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.