2015-07-21 18 views
0

我想在Python運行一個非常簡短的腳本如何使用PYTHONIOENCODING環境變量讓過去一個Unicode解釋問題

from bs4 import BeautifulSoup 
import urllib.request 




html = urllib.request.urlopen("http://dictionary.reference.com/browse/word?s=t").read().strip() 
dhtml = str(html, "utf-8").strip() 
soup = BeautifulSoup(dhtml.strip(), "html.parser") 

我問過類似的問題早了,這個問題一直立足於創建J塞巴斯蒂安對他的回答給予了可敬的評論。 Python program is running in IDLE but not in command line

有沒有一種方法來設置PythonIOEncoding早些時候在任GitHub的Atom或崇高文本2自動編碼soup.prettify()utf-8

我要在服務器上運行這個程序(當然,目前部分只是一個快速測試)

回答

0

s=soup.prettify().encode('utf8')顯式地使它成爲UTF-8。

設置PYTHONIOENCODING=utf8在shell中,然後print(soup.prettify())應隱式使用指定的編碼。