-1

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.

austin-schick
  • 1,225
  • 7
  • 11

3 Answers3

2

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
VicMan
  • 184
  • 5
0

In Python 3.x, the urllib.urlopen function was moved to urllib.request.urlopen:

>>> from urllib.request import urlopen
>>> urlopen
<function urlopen at 0x020BF4B0>
>>>
0

you must use urllib.request.urlopen

timgeb
  • 76,762
  • 20
  • 123
  • 145