How to include RSS feed in RSS?

According to the feedparser documentation , I can turn the RSS feed into a parsed object as follows:

import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')

but I cannot find anything showing how to go the other way; I would like to be able to manipulate "d" and then output the result as XML:

print d.toXML()

but nothing seems to be happening in feedparser to move in that direction. Will I have to iterate over various elements, or is there a faster way?

+3
source share
4 answers

- , - , PyRSS2Gen. ( , -, , , parsed_feed ['feed'] ['image']).

. , ( - 100 , ..)

#!/usr/bin/env python
import datetime

# http://www.feedparser.org/
import feedparser
# http://www.dalkescientific.com/Python/PyRSS2Gen.html
import PyRSS2Gen

# Get the data
parsed_feed = feedparser.parse('http://reddit.com/.rss')

# Modify the parsed_feed data here

items = [
    PyRSS2Gen.RSSItem(
        title = x.title,
        link = x.link,
        description = x.summary,
        guid = x.link,
        pubDate = datetime.datetime(
            x.modified_parsed[0],
            x.modified_parsed[1],
            x.modified_parsed[2],
            x.modified_parsed[3],
            x.modified_parsed[4],
            x.modified_parsed[5])
        )

    for x in parsed_feed.entries
]

# make the RSS2 object
# Try to grab the title, link, language etc from the orig feed

rss = PyRSS2Gen.RSS2(
    title = parsed_feed['feed'].get("title"),
    link = parsed_feed['feed'].get("link"),
    description = parsed_feed['feed'].get("description"),

    language = parsed_feed['feed'].get("language"),
    copyright = parsed_feed['feed'].get("copyright"),
    managingEditor = parsed_feed['feed'].get("managingEditor"),
    webMaster = parsed_feed['feed'].get("webMaster"),
    pubDate = parsed_feed['feed'].get("pubDate"),
    lastBuildDate = parsed_feed['feed'].get("lastBuildDate"),

    categories = parsed_feed['feed'].get("categories"),
    generator = parsed_feed['feed'].get("generator"),
    docs = parsed_feed['feed'].get("docs"),

    items = items
)


print rss.to_xml()
+5

XML, , , python, , RSS.py , ( RSS RSS 1.0). , ..

+1
from xml.dom import minidom

doc= minidom.parse('./your/file.xml')
print doc.toxml()

The only problem is that it does not download feeds from the Internet.

0
source

As a feed creation method, how about PyRSS2Gen ? :)

I have not played with FeedParser, but have you tried just doing str (yourFeedParserObject)? I was often surprised by the various modules that str methods have to simply output the object as text.

[Edit] Just tried the str () method and it does not work on this. It is worth noting that :-)

0
source

Source: https://habr.com/ru/post/1698022/


All Articles