python3.x如何从互联网获取想要的文章,及转化为nltk可以处理的文本
来源:互联网 发布:板绘用什么软件 编辑:程序博客网 时间:2024/06/04 00:32
from urllib.request import urlopen
from bs4 import BeautifulSoup
from nltk import word_tokenize
import nltk
#2种方式解析HTML中的文本
url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read().decode('utf8')
print(html[:60])
#print(html)
#第一种,使用find和rfind查找到文本的开始位置和结束位置,使用python的切片功能
raw = BeautifulSoup(html,'lxml').get_text()#如果出现bs4.FeatureNotFound:(没有安装解析器只需在cmd下:pip install lxml即可)
tokens = word_tokenize(raw)
print(tokens[:10])#检测HTML文本
print(raw.find("Blondes 'to die out in 200 years'"))
print(raw.rfind("The frequency of blondes may drop but they won't disappear."))
#print(raw[22:2449])打印整篇报道
#第二种,使用beautifulsoup自带的功能,匹配到相应的模块,输出文本内容(此时不包含标题,如果需要还需再找相应的模块)
bs = BeautifulSoup(html,'lxml')
print(bs.find("div",class_='bodytext').get_text())
#过滤无关内容
tokens = tokens[110:390]
text = nltk.Text(tokens)#把文本转化为nltk文本进行后续处理
print(text.concordance('gene'))
from bs4 import BeautifulSoup
from nltk import word_tokenize
import nltk
#2种方式解析HTML中的文本
url = "http://news.bbc.co.uk/2/hi/health/2284783.stm"
html = urlopen(url).read().decode('utf8')
print(html[:60])
#print(html)
#第一种,使用find和rfind查找到文本的开始位置和结束位置,使用python的切片功能
raw = BeautifulSoup(html,'lxml').get_text()#如果出现bs4.FeatureNotFound:(没有安装解析器只需在cmd下:pip install lxml即可)
tokens = word_tokenize(raw)
print(tokens[:10])#检测HTML文本
print(raw.find("Blondes 'to die out in 200 years'"))
print(raw.rfind("The frequency of blondes may drop but they won't disappear."))
#print(raw[22:2449])打印整篇报道
#第二种,使用beautifulsoup自带的功能,匹配到相应的模块,输出文本内容(此时不包含标题,如果需要还需再找相应的模块)
bs = BeautifulSoup(html,'lxml')
print(bs.find("div",class_='bodytext').get_text())
#过滤无关内容
tokens = tokens[110:390]
text = nltk.Text(tokens)#把文本转化为nltk文本进行后续处理
print(text.concordance('gene'))
阅读全文
0 0
- python3.x如何从互联网获取想要的文章,及转化为nltk可以处理的文本
- 将当前时间戳转化为 struct tm 结构 然后获取自己想要的数据
- 使用sed把纯文本文章转化为样式自定义的CSDN博客文章
- python3.x的异常处理
- NLTK文本分割器是如何工作的
- python3中将`&#x`(《新)的字符串转化为utf-8
- 如何更有效的从互联网获取信息?
- 想要看的文章
- python3如何将input输入的字符串转化为表达式并计算出结果
- SQL injection 1: 如何从数据库中获取想要获得的内容?
- 【互联网高手教你如何搜集你想要的信息】
- 互联网高手教你如何搜集你想要的信息
- 互联网高手教你如何搜集你想要的信息
- python3如何读取含有中文的文本
- Python3下的NLTK及nltk_data安装问题(Ubuntu环境)
- 从时间服务器获取时间(linux) + Objective-C将获取的值转化为NSDate
- 单个汉字转化为拼音,也可以获取汉字的首个字母
- pyspark+nltk处理文本数据
- Logistic回归
- word2vec 中的数学原理背景知识详解(1)
- dql分页页面显示页码
- 第11周项目1-图的基本算法库
- xsd配置
- python3.x如何从互联网获取想要的文章,及转化为nltk可以处理的文本
- ubuntu14.04 图形化配置 静态IP
- 由参加领域驱动设计大会与自己所想的
- BeanNotOfRequiredTypeException: Bean named "" must be of type [] but was actually of type [com.su
- 1到n的阶层
- dubbo zookeeper简单的provide 和cosumer
- fedora27 sublime3 fcitx 中文输入 解决
- mongodb遍历万亿级数据,论索引的重要性
- 高可用集群架构maxscale