2013-03-19 83 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.