Python parse RSS (feedparser)

Source: Internet
Author: User

Although today's blogs are not as popular as those of the past few years, RSS is still very creative and practical. RSS is a simple XML format used to distribute summaries of content on websites. It can be used to share a variety of information. About RSS detailed information in (http://www.rssboard.org/rss-profile), here detailed description of the meaning of a variety of values (although each version of the support is not the same, but still more universal ). Here I will first introduce how to use feedparser to operate RSS and then introduce some common attributes, which is also convenient for your experiment:
1. feedparser Installation
The first step is to download the source code for compilation and installation. In addition, pip and easy-install can also be used for installation (the magic is that my own python directly has this module ...)
Ii. Use of feedparser
I have to say that feedparser is very powerful not only because it has good compatibility with various rss versions, but also because it is easy to use, just a function (such as fake package replacement)
Code:

1  d = feedparser.parse('http://feed.cnblogs.com/blog/u/161528/rss')

We can see that there is only one parse function. Here, the rss address of my blog is passed in. At this time, all the rss content is in this d.
Take rss in the blog Park as an example to introduce the meaning of some values (personal understanding may lead to incorrect corrections)

First, give the simplified rss of my blog

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <feed xmlns = "http://www.w3.org/2005/Atom"> 3 <title type = "text"> blog garden _ mrbean </title> 4 <subtitle type = "text"> ***** * ***************** </subtitle> 5 <id> uuid: 32303acf-fb5f-4538-a6ba-7a1ac4fd7a58; id = 8434 </id> 6 <updated> 2014-05-14T15: 13: 36Z </updated> 7 <author> 8 <name> mrbean </name> 9 <uri> http://www.cnblogs.com/MrLJC/ </uri> 10 </author> 11 <generator> feed.cnblogs.com </generator> 12 <entry> 13 <id> http: // w Ww.cnblogs.com/MrLJC/p/3715783.html </id> 14 <title type = "text"> Use python to read and write excel (xlrd, xlwt) -mrbean </title> 15 <summary type = "text"> recently, you need to use various methods to complete multiple excel tables... </summary> 16 <published> 2014-05-08T16: 25: 00Z </published> 17 <updated> 2014-05-08T16: 25: 00Z </updated> 18 <author> 19 <name> mrbean </name> 20 <uri> http://www.cnblogs.com/MrLJC/ </uri> 21 </author> 22 <link rel = "alternate" href = "http://www.cnblogs.com/MrLJC/p/ 3715783. html "/> 23 <link rel =" alternate "type =" text/html "href =" http://www.cnblogs.com/MrLJC/p/3715783.html "/> 24 <content type =" html "> recently in an excel table, Sort data in various ways, although I used java to do this kind of things, I decided to try it with python because I was learning python recently. Python is much simpler. Here is a simple record. (Because it is used to learn what, so not too deep, master do not spray, welcome to guide) I. read an excel table to read excel to use... & lt; img src = "http://counter.cnblogs.com/blog/rss/3715783" width = "1" height = "1" alt = ""/& gt; & lt; br/& gt; & lt; p & gt; Link: & lt; a href = "http://www.cnblogs.com/MrLJC/p/3715783.html" target = "_ blank" & gt; Use python to read and write excel (xlrd, xlwt) & lt; /a & gt;. For more information, see. & Lt;/p & gt; </content> 25 </entry> 26 </feed>

 

The value in this section is that most of the content we get is known words, of course, many of which are the builder of the blog Park. To put it simply, I don't quite understand a few.

1. generator: This is the tool used to generate the subscription source, that is, the generator provided by the blog park itself.

2. entry)

3. content: Does the content seem to be html-based? The rss reader displays the content.

The following code demonstrates how to query the content:

1 >>> import feedparser 2 >>> feed = feedparser. parse ("test. xml ") 3 >>> print feed. feed. title 4 blog _mrbean 5 >>> print feed ['feed'] ['title'] 6 blog _mrbean 7 >>> print feed. entries [0]. id # Pay attention to this 8 http://www.cnblogs.com/MrLJC/p/3715783.html 9 >>> print feed. entries [0]. content10 [{'base': u'', 'type': u'text/html', 'value ': u' \ u6700 \ u8fd1 \ u9700 \ u8981 \ u4ece \ u591a \ large \ u8868 \ u91cc \ u9762 \ u7528 \ large \ u79cd \ large \ u6574 \ large \ u4e00 \ large \ u6570 \ Alibaba \ u8bf4 \ u539f \ u6765 \ u7528 \ Alibaba \ u505a \ u8fd9 \ u7c7b \ u4e8b \ u60c5 \ Alibaba \ u662f \ u7531 \ Alibaba \ u6700 \ u8fd1 \ u5728 \ Alibaba \ u6240 \ Alibaba \ u51b3 \ Alibaba \ u7528python \ u5c1d \ u8bd5 \ u4e00 \ Alibaba \ u4e86 \ u3002 \ u53d1 \ Users \ Alibaba \ u7136 \ u7b80 \ u6d01 \ u5f88 \ u591a \ u3002 \ u8fd9 \ u91cc \ u7b80 \ Alibaba \ u3002 \ uff08 \ u7531 \ \ u662f \ u7528 \ u5230 \ u4ec0 \ Alibaba \ u6240 \ Alibaba \ u6df1 \ u5165 \ uff0c \ u9ad8 \ u624b \ u52ff \ u55b7 \ uff0c \ u6b22 \ u8fce \ u6307 \ u5bfc \ uff09 \ u4e00 \ u3001 \ u8bfbexcel \ u8868 \ u8bfbexcel \ u8981 \ u7528...  <br/> <p> \ u672c \ u6587 \ u94fe \ u63a5 \ uff1a <a href = "http://www.cnblogs.com/MrLJC/p/3715783.html" target = "_ blank"> \ u7528python \ u8bfb \ u5199excel \ uff08xlrd \ Alibaba \ uff09 </a> \ uff0c \ Users \ u8f7d \ u8bf7 \ u6ce8 \ u660e \ u3002 </p> ', 'language': None}]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.