python3.6 URLLIB Request

0

I have this code:

import urllib.request

x = urllib.request.urlopen('https://www.google.com')
print (x.read())

And the error that happens:

  

Traceback (most recent call last): File "python", line 3, in    urllib.error.URLError:

It seems that urllib.request no longer has the function urlopen ...

Can anyone help?

    
asked by anonymous 08.11.2017 / 18:22

1 answer

0

Friend,

Although you speak python3, you are using python2 at the time of running the script!

Your code worked perfectly on my python3.6! Now the procedure changes a little if you use python2, you have to use the module: urllib2 and follows:

import urllib2
req = urllib2.Request('http://www.google.com')
response = urllib2.urlopen(req)
pagina = response.read()
print(pagina)

In this way you reach the same goal in python2.7! See if my answer answers.

    
21.05.2018 / 19:47