《python进行自然语言处理》练习处理HTML内容的时候出现ImportError: No module named BeautifulSoup错误
2015-12-30 17:20
671 查看
在练习Python自然语言处理的时候,遇到了下面的错误:
原来Beautifusoup4改名为bs4了,所以把:
改为:
但是,改完之后,运行又会出现:
NotImplementedError: To remove HTML markup, use BeautifulSoup's get_text() function
原来是使用clean_html()函数出错,网站:http://stackoverflow.com/questions/10524387/beautifulsoup-get-text-does-not-strip-all-tags-and-javascript介绍:
以后的版本,似乎不支持clean_html()和clean_url()这两个函数
Support for clean_html and clean_url will be dropped for future versions of nltk. Please use BeautifulSoup for now...it's very unfortunate.
然后我查了一下Beautiful Soup 4.2.0 文档,才发现,BeautifulSoup使用已经发生了改变,使用方式参照上面的例子,然后最终程序:
然后就ok了!!!
python3令人头疼但快乐着。
# -*- coding: utf-8 -*- """ Created on Wed Dec 30 17:10:30 2015 @author: mahao """ #错误的形式 import nltk from urllib.request import urlopen from bs4 import BeautifulSoup url = "http://news.bbc.co.uk/2/hi/health/2284783.stm" html = urlopen(url).read() #print(html[:60]) raw = nltk.clean_html(html) #print(nltk.word_tokenize(raw)) tokens = nltk.word_tokenize(raw) tokens=tokens[96:399] text=nltk.Text(tokens) print(text.concordance('gene')) """ <span style="color:#ff0000;"><span style="color:#ff0000;">报错:ImportError: No module named 'BeautifulSoup</span>'</span> """最后参考了Beautiful Soup 4.2.0 文档,看到了:
原来Beautifusoup4改名为bs4了,所以把:
改为:
但是,改完之后,运行又会出现:
NotImplementedError: To remove HTML markup, use BeautifulSoup's get_text() function
原来是使用clean_html()函数出错,网站:http://stackoverflow.com/questions/10524387/beautifulsoup-get-text-does-not-strip-all-tags-and-javascript介绍:
以后的版本,似乎不支持clean_html()和clean_url()这两个函数
Support for clean_html and clean_url will be dropped for future versions of nltk. Please use BeautifulSoup for now...it's very unfortunate.
然后我查了一下Beautiful Soup 4.2.0 文档,才发现,BeautifulSoup使用已经发生了改变,使用方式参照上面的例子,然后最终程序:
import nltk from urllib.request import urlopen from bs4 import BeautifulSoup url = "http://news.bbc.co.uk/2/hi/health/2284783.stm" html = urlopen(url).read() #print(html[:60]) soup = BeautifulSoup(html) raw = soup.get_text() #print(nltk.word_tokenize(raw)) tokens = nltk.word_tokenize(raw) tokens=tokens[96:399] text=nltk.Text(tokens) print(text.concordance('gene'))
然后就ok了!!!
python3令人头疼但快乐着。
相关文章推荐
- 我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)
- 我的Python成长之路---第一天---Python基础(作业1:登录验证)---2015年12月26日(雾霾)
- python ---用户输入
- python --study address
- python读取xml文件
- Python实现批处理
- python中一些实用而有趣的模块
- 笨方法学Python(16-20)
- Python学习笔记——命令行下的输入输出
- (ubuntu环境下)浏览器(通过cgi)调用外部程序
- 解决Python代码编码问题 SyntaxError: Non-UTF-8 code starting with '\xc1'
- python 并发编程(多进程、多线程、gevent)
- [LeetCode]题解(python):094-Binary Tree Inorder Traversal
- python实现的 K-近邻算法代码详细解释
- Python的Cython在Windows环境下的部署安装
- [LeetCode]题解(python):093-Restore IP Addresses
- 域名解析正确性校验脚本(python多进程)
- Python爬虫学习笔记——豆瓣登陆(二)
- python 简易ss端口管理器
- [LeetCode]题解(python):092-Reverse Linked List II