Python Forum
Error: module 'urllib' has no attribute 'urlopen' - 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: Error: module 'urllib' has no attribute 'urlopen' (/thread-15740.html)



Error: module 'urllib' has no attribute 'urlopen' - mitmit293 - Jan-29-2019

Error: module 'urllib' has no attribute 'urlopen'


What is it then?

(beginner)


RE: Error: module 'urllib' has no attribute 'urlopen' - perfringo - Jan-29-2019

There is batteries-included urllib.request described as 'Extensible library for opening URLs' in Python documentation.

If you do:
import urllib.request

then you can access urlopen like this:

urllib.request.urlopen()


Alternatively just urlopen (if using from urllib.request import urlopen)


RE: Error: module 'urllib' has no attribute 'urlopen' - snippsat - Jan-29-2019

The import has changed in Python 3.
>>> import urllib.request
>>> 
>>> req = urllib.request.urlopen('https://www.python.org/')
But i would advice to not use urllib at all,use Requests .
>>> import requests
>>> 
>>> req = requests.get('https://www.python.org/')
>>> req.status_code
200
>>> req.encoding
'utf-8'
A typically example to load a site and do web-scraping.
from bs4 import BeautifulSoup
import requests

url = 'https://www.python.org/'
url_get = requests.get(url)
soup = BeautifulSoup(url_get.content, 'lxml')
print(soup.select('head > title')[0].text)
Output:
Welcome to Python.org