日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 中 feedparser的简单用法

發布時間:2024/9/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 中 feedparser的简单用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在機器學習實戰中用到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的简单用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。