作者alvin17 (alvin17)
看板Python
标题[问题] BeautifulSoup中搜寻中文
时间Wed Feb 13 02:50:49 2008
我想用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