[問題] BeautifulSoup中搜尋中文

看板Python作者 (alvin17)時間17年前 (2008/02/13 02:50), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/4 (看更多)
我想用BeautifulSoup來parse含有中文的頁面 但是一直出錯 parse.py該段的code如下: soup = BeautifulSoup.BeautifulSoup(page, fromEncoding="utf-8") searchlist = soup.html.body('div') print 'Size:', len(searchlist) for i in range(len(searchlist)): print i if searchlist[i].findAll('img', title="統計"): print searchlist[i] 用IDLE執行出現if searchlist[i].findAll('img', title="統計"):這行有錯誤 錯誤敘述為 UnicodeDecodeError: 'cp950' codec can't decode bytes in position 4-5: illegal multibyte sequence 但是我直接在python shell裡面打 >>> searchlist[0].findAll('img', title="統計") [<img id="n4" src="files/x.gif" title="統計" />] 可以正確找到我想要的 我嘗試把parse.py的code從 if searchlist[i].findAll('img', title="統計"): 換成 if searchlist[i].findAll('img', u'title="統計"'): 則parse.py執行不會出錯 但是我就找不到要的HTML code @@ 請問前輩們 我的code哪裡有錯? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.5.84
文章代碼(AID): #17iUgAhS (Python)
文章代碼(AID): #17iUgAhS (Python)