問題:在Python3.4中 運作如下代碼報錯,找不到urllib2
import urllib2
response = urllib2.urlopen('http://www.baidu.com/')
print response.read()
解決辦法:
import urllib.request
resp=urllib.request.urlopen('http://www.baidu.com')
html=resp.read()
print(html)
在python3.4裡面,用urllib.request代替urllib2,另外python3之後,不能再用
print html
這樣的方式,因為print這個時候已經是一個方法了。必須使用下面的方法
print(html)