Python Forum

Full Version: Error: module 'urllib' has no attribute 'urlopen'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Error: module 'urllib' has no attribute 'urlopen'


What is it then?

(beginner)
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)
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