最近在机器学习实战中用到feedparser ,然后简单总结了一下:

feedparser是python中最常用的RSS程序库,使用它我们可轻松地实现从任何 RSS 或 Atom 订阅源得到标题、链接和文章的条目。

首先随便找了一段简化的rss:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_mrbean</title><subtitle type="text">**********************</subtitle><id>uuid:32303acf-fb5f-4538-a6ba-7a1ac4fd7a58;id=8434</id><updated>2014-05-14T15:13:36Z</updated><author><name>mrbean</name><uri>http://www.cnblogs.com/MrLJC/</uri></author><generator>feed.cnblogs.com</generator><entry><id>http://www.cnblogs.com/MrLJC/p/3715783.html</id><title type="text">用python读写excel(xlrd、xlwt) - mrbean</title><summary type="text">最近需要从多个excel表里面用各种方式整...</summary><published>2014-05-08T16:25:00Z</published><updated>2014-05-08T16:25:00Z</updated><author><name>mrbean</name><uri>http://www.cnblogs.com/MrLJC/</uri></author><link rel="alternate" href="http://www.cnblogs.com/MrLJC/p/3715783.html" /><link rel="alternate" type="text/html" href="http://www.cnblogs.com/MrLJC/p/3715783.html" /><content type="html">最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了。发现python果然简洁很多。这里简单记录一下。(由于是用到什么学什么,所以不算太深入,高手勿喷,欢迎指导)一、读excel表读excel要用...&lt;img src="http://counter.cnblogs.com/blog/rss/3715783" width="1" height="1" alt=""/&gt;&lt;br/&gt;&lt;p&gt;本文链接:&lt;a href="http://www.cnblogs.com/MrLJC/p/3715783.html" target="_blank"&gt;用python读写excel(xlrd、xlwt)&lt;/a&gt;,转载请注明。&lt;/p&gt;</content></entry>
</feed>

把他复制到一个.txt文件中,保存为.xml:

import feedparserprint feedparser.parse('')
d=feedparser.parse("tt.xml")
print d['feed']['title']
print d.feed.title        # 通过属性访问
print d.entries[0].id
print d.entries[0].content

结果:

{'feed': {}, 'encoding': u'utf-8', 'bozo': 1, 'version': u'', 'namespaces': {}, 'entries': [], 'bozo_exception': SAXParseException('no element found',)}
博客园_mrbean
博客园_mrbean
http://www.cnblogs.com/MrLJC/p/3715783.html
[{'base': u'', 'type': u'text/html', 'value': u'\u6700\u8fd1\u9700\u8981\u4ece\u591a\u4e2aexcel\u8868\u91cc\u9762\u7528\u5404\u79cd\u65b9\u5f0f\u6574\u7406\u4e00\u4e9b\u6570\u636e\uff0c\u867d\u7136\u8bf4\u539f\u6765\u7528\u8fc7java\u505a\u8fd9\u7c7b\u4e8b\u60c5\uff0c\u4f46\u662f\u7531\u4e8e\u6700\u8fd1\u5728\u5b66python\uff0c\u6240\u4ee5\u5f53\u7136\u5c31\u51b3\u5b9a\u7528python\u5c1d\u8bd5\u4e00\u4e0b\u4e86\u3002\u53d1\u73b0python\u679c\u7136\u7b80\u6d01\u5f88\u591a\u3002\u8fd9\u91cc\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b\u3002\uff08\u7531\u4e8e\u662f\u7528\u5230\u4ec0\u4e48\u5b66\u4ec0\u4e48\uff0c\u6240\u4ee5\u4e0d\u7b97\u592a\u6df1\u5165\uff0c\u9ad8\u624b\u52ff\u55b7\uff0c\u6b22\u8fce\u6307\u5bfc\uff09\u4e00\u3001\u8bfbexcel\u8868\u8bfbexcel\u8981\u7528...<img alt="" height="1" src="http://counter.cnblogs.com/blog/rss/3715783" width="1" /><br /><p>\u672c\u6587\u94fe\u63a5\uff1a<a href="http://www.cnblogs.com/MrLJC/p/3715783.html" target="_blank">\u7528python\u8bfb\u5199excel\uff08xlrd\u3001xlwt\uff09</a>\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u3002</p>', 'language': None}]
**********************

feedparser 最为核心的函数自然是 parse() 解析 URL 地址的函数,返回的形式如:

{'feed': {}, 'encoding': u'utf-8', 'bozo': 1, 'version': u'', 'namespaces': {}, 'entries': [], 'bozo_exception': SAXParseException('no element found',)}

简单实例:

每个 RSS 和 Atom 订阅源都包含一个标题(d.feed.title)和一组文章条目(d.entries)。

通常,每个文章条目都有一段摘要(d.entries[i].summary),或者是包含了条目中实际文本的描述性标签(d.entries[i].description)

import feedparser#print feedparser.parse('')
d=feedparser.parse("http://blog.csdn.net/lanchunhui/rss/list")
# d.feed
print d['feed']['title']
print d.feed.title        # 通过属性访问
print d.feed.link
print d.feed.subtitle  #d.entries
print type(d.entries)
print len(d.entries)
print 'e.title:',[e.title for e in d.entries][:3]# d.entries[0].summary是第一篇文章的摘要信息
print 'description==?description:',d.entries[0].description==d.entries[0].summary

结果:

计算机科学与艺术
计算机科学与艺术
http://blog.csdn.net/lanchunhui
Email:zch921005@126.com
<type 'list'>
20
e.title: [u'[\u539f]Handle/Body pattern\uff08Wrapper pattern\uff09', u'[\u539f]Java \u5de5\u7a0b\u4e0e Eclipse \u9ad8\u7ea7\u7528\u6cd5', u'[\u539f]Java \u4e0b\u7684\u51fd\u6570\u5bf9\u8c61']
description==?description: True

其实spyder的variable explorer 窗口可视化的显示了数据的结构:

其他基本用法:
参考:

http://www.cnblogs.com/youxin/archive/2013/06/12/3132713.html

>>> import feedparser
>>> d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml")
>>> d['feed']['title']             # feed data is a dictionary
u'Sample Feed'
>>> d.feed.title                   # get values attr-style or dict-style
u'Sample Feed'
>>> d.channel.title                # use RSS or Atom terminology anywhere
u'Sample Feed'
>>> d.feed.link                    # resolves relative links
u'http://example.org/'
>>> d.feed.subtitle                 # parses escaped HTML
u'For documentation <em>only</em>'
>>> d.channel.description          # RSS terminology works here too
u'For documentation <em>only</em>'
>>> len(d['entries'])              # entries are a list
>>> d['entries'][0]['title']       # each entry is a dictionary
u'First entry title'
>>> d.entries[0].title             # attr-style works here too
u'First entry title'
>>> d['items'][0].title            # RSS terminology works here too
u'First entry title'
>>> e = d.entries[0]
>>> e.link                         # easy access to alternate link
u'http://example.org/entry/3'
>>> e.links[1].rel                 # full access to all Atom links
u'related'
>>> e.links[0].href                # resolves relative links here too
u'http://example.org/entry/3'
>>> e.author_detail.name           # author data is a dictionary
u'Mark Pilgrim'
>>> e.updated_parsed              # parses all date formats
(2005, 11, 9, 11, 56, 34, 2, 313, 0)
>>> e.content[0].value             # sanitizes dangerous HTML
u'<div>Watch out for <em>nasty tricks</em></div>'
>>> d.version                      # reports feed type and version
u'atom10'
>>> d.encoding                     # auto-detects character encoding
u'utf-8'
>>> d.headers.get('Content-type')  # full access to all HTTP headers
u'application/xml'

附:spyder ipython 中文乱码的问题

import sys
reload(sys)
sys.setdefaultencoding('utf8')

即可解决。。。

python 中 feedparser的简单用法相关推荐

  1. Python中的super()简单用法

    如果在子类中也定义了_init_()函数,那么该如何调用基类的_init_()函数: 方法一.明确指定 : class C(P):def __init__(self):P.__init__(self) ...

  2. python中parse.add_argument()简单用法

    argparse模块简单使用流程以及与命令行的交互 1.导入模块 2.创建解析器 3.添加参数 4.解析参数 5使用参数 完整代码 与命令行的交互 argparse 模块是 Python 内置的一个用 ...

  3. python中retry的简单用法

    一.简介 retry是一个用于错误处理的模块,功能类似try-except,但更加快捷方便,本文就将简单地介绍一下retry的基本用法. 二.基本用法 retry-作为装饰器进行使用,不传入参数时功能 ...

  4. python count函数代码_python中count函数简单用法

    python中count函数简单用法 python中count函数的用法 Python count()方法 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符 ...

  5. python中的lambda函数用法--无需定义函数名的函数或子程序,避免代码充斥着大量单行函数

    匿名函数lambda:是指一类无需定义标识符(函数名)的函数或子程序. lambda 函数可以接收任意多个参数 (包括可选参数) 并且返回单个表达式的值. 要点: lambda 函数不能包含命令 包含 ...

  6. python中break怎么用_详解Python中break语句的用法

    详解Python中break语句的用法 在Python中的break语句终止当前循环,继续执行下一个语句,就像C语言中的break一样. break最常见的用途是当一些外部条件被触发,需要从一个循环中 ...

  7. python中循环语句的用法,python几种循环方式

    python中while循环的用法是什么? python while循环语句:while 判断条件(condition):执行语句(statements)--执行语句可以是单个语句或语句块. 判断条件 ...

  8. python中struct.unpack的用法

    python中struct.unpack的用法 4/25/2009 12:18:21 PM Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的. 1. 设置 ...

  9. python中divmod函数的用法_Python中divmod函数的用法

    Python中divmod函数的用法,语言,余数,是一种,面向对象,函数 Python中divmod函数的用法 Python中divmod函数的用法 在Python中divmod函数的作用是把除数和余 ...

最新文章

  1. 反射 -- 通过字符串操作对象中的成员
  2. jQuery里面的datepicker日期控件默认是显示英文的,如何显示中文或其他语言呢?...
  3. Linux高性能网络:协程系列09-协程性能测试
  4. 解决react-router4在browserhistory路由下的nginx的白屏或者404问题
  5. 清除vs2005起始页最近打开项目
  6. VIM配置ActionScript
  7. Rust:集所有语言之大成者
  8. 无法上网dns转发_苹果笔记本上网很慢怎么回事?macbook无线上网慢的解决方法...
  9. ospf hello时间和dead_网络工程师_思科 | OSPF由简到难,配合命令学
  10. 初识C++之函数重载、重写、重定义的区别
  11. php phpexcel用法,PHPExcel用法总结
  12. siteminder sso agent 初探
  13. 条款三 : 操作符is或as优于强制转型
  14. Java中的异常处理与抛出
  15. 嗅探辅助利器-幻影网盾原理
  16. 关掉计算机usb接口,bios中不小心把电脑的USB接口给关闭了,怎么样开启
  17. Wordpress主题制作之首页
  18. 第二次作业—时事点评
  19. 经纬度转GeoHash
  20. ls基本用法-查看文件大小 k m g

热门文章

  1. OpenCV 错误级别分析ELA的实例(附完整代码)
  2. c++median search中位数搜索的实现算法(附完整源码)
  3. C++happy number开心数的实现算法(附完整源码)
  4. C语言实现hash/adler32算法(附完整源码)
  5. fatal error C1189: #error:MFC does not support WINVER less than 0x0501. Please change the definitio
  6. 经典C语言程序100例之五一
  7. php不支持定时器么,PHP没有定时器?
  8. mysql输入错误怎样更正_HotDB MySQL 篇| MySQL 源码系列的补充与更正
  9. 3.TF-IDF算法介绍、应用、NLTK实现TF-IDF算法、Sklearn实现TF-IDF算法、算法的不足、算法改进
  10. 批量查询,mget语法,mget批量查询(来自学习资料,第26节)