python---easy_install 安裝軟件 及BeautifulSoup 中文亂碼

安裝easy_install工具

使用ez_setup.py腳本方便安裝:python

wget  - q http: // peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py


安裝完後,最好確保easy_install所在目錄已經被加到PATH環境變量裏:
shell

Windows: C:\Python25\Scripts
Linux: 
/ usr / local / bin


安裝 Mechanize 和 BeautifulSoup

安裝Mechanize 和 BeautifulSoup,能夠執行以下命令,系統會自動在pypi網站列表裏查找相關軟件包:

easy_install Mechanize
easy_install BeatifulSoup


BeautifulSoup中文亂碼

簡單的一段代碼:


from  BeautifulSoup  import  BeautifulSoup 

page 
=   " <a><s>中文</s></a> "
soup 
=  BeautifulSoup(page, fromEncoding = " utf-8 " )
print  soup
=  open( " ss.txt " " w+ " )
f.write(str(soup))
f.close()
打印出來的亂碼,寫入文件倒是正常的。查了一下,這個跟shell的編碼有關,windows的通常是gbk,因此utf-8出亂碼很正常,若是要正常打印,只要把字符串轉換成指定編碼方式就能夠。
相關文章
相關標籤/搜索