I have the following line of code
sock = urllib.urlopen (url)
This line works perfectly in python 2.x, but when I run it with python 3.x it does not work.
I have the following line of code
sock = urllib.urlopen (url)
This line works perfectly in python 2.x, but when I run it with python 3.x it does not work.
For python 3.x you should write like this:
import urllib.request
sock = urllib.request.urlopen (url)
print (sock.read (200))
In python 2.x: urllib.urlopen In python 3.x: urllib.request.urlopen
print (sock.read (200)) displays the first 200 bytes of the contents of 'url'
Finally close the connection:
sock.close
In Python 3.x, the urllib.urlopen
function was moved to urllib.request.urlopen
:
>>> from urllib.request import urlopen
>>> urlopen
<function urlopen at 0x020BF4B0>
>>>