python3.6 URLLIB Request

Asked

Viewed 852 times

1

I have this code:

import urllib.request

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

And the mistake 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 ...

Anyone can help?

1 answer

0

Friend,

Although you speak python3, you are using python2 when 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 follow:

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

This way you achieve the same goal in python2.7! See if my answer answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.