Reputation: 61
I am just getting my feet wet in the art of webscraping and I am following the tutorials from this source. For some reason I cannot import the error module from 'urllib' to handle exceptions. Since this is a built-in library, I am confused as to why this is an issue.
from urllib import urlopen
from urllib.error import HTTPError
from urllib.error import URLError
yields the error
ImportErrorTraceback (most recent call last)
<ipython-input-1-30b72b3bf2ea> in <module>()
1 from urllib import urlopen
----> 2 from urllib.error import HTTPError
3 from urllib.error import URLError
I have tried the same code with another IDE (IntelliJ) and it works as expected leading me to believe that this could be an issue with Google Colab itself. Could someone weight in and possibly help me find a solution to this problem.
I am new to programming, so if this is a juvenile question or if this is not the appropriate place for this question, I apologize in advance.
P.S. I have double checked that the runtime is Python 3
Upvotes: 3
Views: 5474
Reputation: 2972
You are trying to run this code using Python 2. Use Python 3 and it will work.
Python 2:
>>> from urllib.error import HTTPError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named error
>>>
Python 3:
>>> from urllib.error import HTTPError
>>>
Upvotes: 0
Reputation: 498
Just try this:
from urllib.request import urlopen
Always remember to try to search the docs of a particular library it helps a lot.
Upvotes: 2
Reputation: 7412
Your problem is in
from urllib import urlopen
Right way to import urlopen
is from urllib.request
from urllib.request import urlopen
Upvotes: 3