2013-03-19 80 views
0

我正在使用urllib制作python http横幅抓取器,但它给了我一个错误。使用urllib模块时python错误

我无法导入urllib2(模块未找到错误),所以尝试用urllib代替。

下面是代码,它有什么问题?

#!/usr/bin/env python 
import urllib 
url=urllib.urlopen('www.bing.com') 
print (url.info()) 

错误:

AttributeError: 'module' object has no attribute 'urlopen' 

回答

3

您正在使用Python 3;改为使用urllib.request

import urllib.request 

url = urllib.request.urlopen('http://www.bing.com') 
print(url.info()) 

这是记录在top of the urllib2 page

Note: The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error . The 2to3 tool will automatically adapt imports when converting your sources to Python 3.