python 3.x報錯:No module named 'cookielib'或No module named 'urllib2'

1.    ModuleNotFoundError: No module named 'cookielib'html

 

Python3中,import  cookielib改爲 import  http.cookiejar,而後方法裏cookielib也改爲 http.cookiejar。python

2.    ModuleNotFoundError: No module named 'urllib2'cookie

Python 3中urllib2用urllib.request替代。ide

在Python官方文檔裏面已有說明:google

Note:編碼

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

from urllib.request import urlopencode

response = urlopen("http://www.google.com")htm

html = response.read()blog

print(html)

3.    NameError: name 'raw_input' is not defined

Python 3中用input()替換raw_input()

4.    UserWarning: You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.

注意這句:warnings.warn("You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.") 緣由:python3 缺省的編碼是unicode, 再在from_encoding設置爲utf8, 會被忽視。

Python 3中soup = BeautifulSoup(html_doc, "html.parser", from_encoding="utf-8")這一句中刪除from_encoding="utf-8"

相關文章
相關標籤/搜索