Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python urllib error

so I have this code:

def crawl(self, url):
    data = urllib.request.urlopen(url)
    print(data)

but then when I call the function, it returns

    data = urllib.request.urlopen(url)
AttributeError: 'module' object has no attribute 'request'

what did I do wrong? I already imported urllib..

using python 3.1.3

like image 767
kamikaze_pilot Avatar asked Mar 14 '26 14:03

kamikaze_pilot


2 Answers

In python3, urllib is a package with three modules request, response, and error for its respective purposes.

Whenever you had import urllib or import urllib2 in Python2. Replace them with

import urllib.request
import urllib.response
import urllib.error

The classs and methods are same.

BTW, use 2to3 tool if you converting from python2 to python3.

like image 175
Senthil Kumaran Avatar answered Mar 16 '26 02:03

Senthil Kumaran


urllib.request is a separate module; import it explicitly.

like image 41
Ignacio Vazquez-Abrams Avatar answered Mar 16 '26 03:03

Ignacio Vazquez-Abrams