kamikaze_pilot
kamikaze_pilot

Reputation: 14834

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

Upvotes: 2

Views: 3486

Answers (2)

Senthil Kumaran
Senthil Kumaran

Reputation: 56813

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.

Upvotes: 9

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

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

Upvotes: 2

Related Questions