close

[Solved] AttributeError: ‘module’ object has no attribute ‘urlopen’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘module’ object has no attribute ‘urlopen’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘module’ object has no attribute ‘urlopen’ Error Occurs?

Today I get the following error AttributeError: ‘module’ object has no attribute ‘urlopen’ in python.

How To Solve AttributeError: ‘module’ object has no attribute ‘urlopen’ Error ?

  1. How To Solve AttributeError: 'module' object has no attribute 'urlopen' Error ?

    To Solve AttributeError: 'module' object has no attribute 'urlopen' Error To get 'dataX = urllib.urlopen(url).read()' working in python3 (this would have been correct for python2) you must just change 2 little things.

  2. AttributeError: 'module' object has no attribute 'urlopen'

    To Solve AttributeError: 'module' object has no attribute 'urlopen' Error To get 'dataX = urllib.urlopen(url).read()' working in python3 (this would have been correct for python2) you must just change 2 little things.

Solution 1

Change TWO lines:

import urllib.request #line1

#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2

If You got ERROR 403: Forbidden Error exception try this:

siteurl = "http://www.python.org"

req = urllib.request.Request(siteurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'})
pageHTML = urllib.request.urlopen(req).read()

Solution 2

To get ‘dataX = urllib.urlopen(url).read()‘ working in python3 (this would have been correct for python2) you must just change 2 little things.

1: The urllib statement itself (add the .request in the middle):

dataX = urllib.request.urlopen(url).read()

2: The import statement preceding it (change from ‘import urlib’ to:

import urllib.request

And it should work in python3 🙂

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read