Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
来源:互联网 发布:幸运28软件下载 编辑:程序博客网 时间:2024/05/22 17:20
第 0008 题:一个HTML文件,找出里面的正文。
思路:我把这里的正文理解为网页中我主要内容,那么怎么去抓取这个主要内容呢?我一开始的想法是用beautifulsoup来解析网页,但是又想到如果要抽取正文的话这样做还涉及到比较复杂的算法,而且对于不同的网页来说效果可能做不到很好。后来我发现了Python-goose(Github)这个神器,它是基于NLTK和Beautiful Soup的,分别是文本处理和HTML解析的领导者,目标是给定任意资讯文章或者任意文章类的网页,不仅提取出文章的主体,同时提取出所有元信息以及图片等信息,支持中文网页(用到了结巴分词)。这个正好符合需求,所以直接拿来用了。
安装python goose:
- git clone https://github.com/grangier/python-goose.git
- cd python-goose
- pip install -r requirements.txt
- python setup.py install
0008.提取HTML正文内容.py
#!/usr/bin/env python#coding: utf-8from goose import Goosefrom goose.text import StopWordsChineseimport sysreload(sys)sys.setdefaultencoding("utf-8")# 要分析的网页urlurl = 'http://www.ruanyifeng.com/blog/2015/05/thunk.html'def extract(url): ''' 提取网页正文 ''' g = Goose({'stopwords_class': StopWordsChinese}) article = g.extract(url=url) return article.cleaned_textif __name__ == '__main__': print extract(url)
拿阮一峰博客上一篇文章测试,效果如下:
0 0
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- python show-me-the-code 第0004
- Python Show-Me-the-Code 第 0002 题 MySQL操作
- Python Show-Me-the-Code 第 0003 题 Redis操作
- Python Show-Me-the-Code 第 0004 题 统计单词
- Python Show-Me-the-Code 第 0021题 密码加密
- Python Show-Me-the-Code 第 0001题
- Python Show-Me-the-Code 第 0002题
- python show-me-the-code 第0010题
- python show-me-the-code 第0011题
- python show-me-the-code 第0012题
- Python Show-Me-the-Code 第 0000 题 图片右上角加数字
- Python Show-Me-the-Code 第 0001 题 生成激活码
- Python Show-Me-the-Code 第 0005 题 批量图片处理
- Python Show-Me-the-Code 第 0006 题 最重要的词
- Python Show-Me-the-Code 第 0010 题 生成验证码图片
- Python Show-Me-the-Code 第 0011 题 过滤敏感词
- 最近用的C++模板
- 线程写法
- 【github】project is already on GitHub解决办法
- 愿得一人心,白首不相离!
- 添加故事版
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- leetcode 日经贴,Cpp code -Merge Sorted Array
- 什么是淘宝关键词以及如何查找关键词的方法
- highcharts+json+ashx 图表动态数据绑定
- NYOJ247 虚拟的城市之旅(强联通缩点+dfs)
- Java使用"指针"快速比较字节
- Word文档怎么转为PDF格式
- Nim游戏
- Java注释接口关键词@interface的用法