美文网首页
python自然语言学习第三章(4)

python自然语言学习第三章(4)

作者: 魏小胖 | 来源:发表于2018-07-10 10:28 被阅读0次

依然是第三章的问题,尝试着把html文件去标记,换成text文件,结果遇到不少问题

p93

我用的是python3.6,书上用的是python2.x,所以好多地方代码都会有差异:

>>> from urllib.request import urlopen

>>> url="http://www.gutenberg.org/files/2554/2554-0.txt"

>>> html=urlopen(url).read()

(书上用的raw=nltk.clean_html(html)会出现错误,提示使用 BeautifulSoup.get_text(),但是一般情况下我们都没有装 BeautifulSoup,所以又要回头去装 BeautifulSoup这个包)

>>> from bs4 import BeautifulSoup (哈哈,装好了就导入这个包)

>>>raw=BeautifulSoup(html).get_text()  (如果直接这样用又要错了,“”The code that caused this warning is on line 1 of the file. To get rid of this warning, change code that looks like this:BeautifulSoup(YOUR_MARKUP}) to this: BeautifulSoup(YOUR_MARKUP, "html.parser") )

>>> raw=BeautifulSoup(html,"html.parser").get_text()(这句要写成这样)

相关文章

网友评论

      本文标题:python自然语言学习第三章(4)

      本文链接:https://www.haomeiwen.com/subject/cecfpftx.html