调用BeautifuSoup进行html的文本内容提取问题

tczl1992 2012-11-30 06:30:06
定义一个提取文本的函数
def gettext(html):
from bs4 import BeautifulSoup
soup= BeautifulSoup(html)
return soup.get_text()
这是我下载内容的函数
def downURL(url,filename):
print url
print filename
try:
fp = urllib2.urlopen(url)
except:
print 'download exception'
return 0
op = open(filename,"wb")
while 1:
s = fp.read()
if not s:
break
s=gettext(s)
op.write(s)
fp.close()

return 1
编译的时候就提示
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
还有
UnicodeEncodeError: 'ascii' codec can't encode characters in position 106-124: ordinal not in range(128)
...全文
634 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
crifan 2012-12-10
  • 打赏
  • 举报
回复
chenhaiou553166961 2012-12-03
  • 打赏
  • 举报
回复
op.write(str(s))

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧