How do I turn an RSS feed back into RSS?

According to the feedparser documentation, I can turn an RSS feed into a parsed object like this:

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

but I can't find anything showing how to go the other way; I'd like to be able do manipulate 'd' and then output the result as XML:

print d.toXML()

but there doesn't seem to be anything in feedparser for going in that direction. Am I going to have to loop through d's various elements, or is there a quicker way?


Asked by: Ada438 | Posted: 05-10-2021






Answer 1

Appended is a not hugely-elegant, but working solution - it uses feedparser to parse the feed, you can then modify the entries, and it passes the data to PyRSS2Gen. It preserves most of the feed info (the important bits anyway, there are somethings that will need extra conversion, the parsed_feed['feed']['image'] element for example).

I put this together as part of a little feed-processing framework I'm fiddling about with.. It may be of some use (it's pretty short - should be less than 100 lines of code in total when done..)

#!/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()

Answered by: Kimberly254 | Posted: 06-11-2021



Answer 2

If you're looking to read in an XML feed, modify it and then output it again, there's a page on the main python wiki indicating that the RSS.py library might support what you're after (it reads most RSS and is able to output RSS 1.0). I've not looked at it in much detail though..

Answered by: Patrick340 | Posted: 06-11-2021



Answer 3

from xml.dom import minidom

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

The only problem is that it do not download feeds from the internet.

Answered by: Edward280 | Posted: 06-11-2021



Answer 4

As a method of making a feed, how about PyRSS2Gen? :)

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

[Edit] Just tried the str() method and it doesn't work on this one. Worth a shot though ;-)

Answered by: Stuart857 | Posted: 06-11-2021



Similar questions

python - Turn "abc" to "[97, 98 99]"

how can I change a string to a string of list (as seen in the question header) I can simply do something like that, but I'm there is a simpler way orig = "bla bla" final = "[" for i in orig: final = "%s %d," % (final, i) final = final[:-1] + "]"


How to turn Python code into C++

Closed. This question needs to be more focused. It ...


python - Turn URL into HTML link

In Python, if I have a URL, what is the simplest way to turn something like: http://stackoverflow.com into: <a href="http://stackoverflow.com">http://stackoverflow.com</a> So far I have tied a lot with Regular Expressions, but nothing works at all.


python - How to turn CSV file into list of rows?

I currently have a CSV file saved as Windows comma separated values f = open('file.csv') csv_f = csv.reader(f) for row in lines: print row returns Company,City,State Ciena Corporation,Linthicum,Maryland Inspirage LLC,Gilbert,Arizona Facebook,menlo park,CA I am trying to make a list column by column f = open('file.csv') csv_f = csv...


How can I turn a csv file into a list of list in python

I want to be able to turn csv file into a list of lists with the column values for each list. For example: 6,2,4 5,2,3 7,3,6 into [[6,5,7],[2,2,3],[4,3,6]] Ive only managed to open the file and only having success printing it as rows with open(input,'rb') as csvfile: csv_file = csv.reader(csvfile) header = csv_file.next() ...


python - How to turn dict in a list into a dict

I want to turn the list like this: [{u'host': u'node54', u'key': u'cpu_load_average_limit', u'value': 4.0}, {u'host': u'node54', u'key': u'ram_allocation_ratio', u'value': 4.0}, {u'host': u'node54', u'key': u'cpu_allocation_ratio', u'value': 4.0}, {u'host': u'node53', u'key': u'cpu_load_average_limit', u'value': 4.0}, {u'host': u'node53', u'key': u'ram_allocation_ratio', u'value': 4.0}, {u'host': u...


python - How do I turn data from a text file into a list?

My objective here is to open the dog file, convert it into a list, and then let the user enter a type of dog and if it matches a dog name in the list, say it's correct. dog_file = open("Dogs.txt", "r") dogs = dog_file.readlines() print(dogs) data = input("Enter a name: ") if data == dogs: print("Success") else: print("Sorry that didn't work")


Turn the fan on 10 scs long python

This is my code turning the fan on i run the sleep on separate thread because it makes the entire script sleep def fan_on(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) FAN_PIN = 23 GPIO.setup(FAN_PIN,GPIO.OUT) GPIO.output(FAN_PIN,True) t = Thread(target=sleep_fan) t.deamon = True t.start() def sleep_fan(): time.sleep(10) The script is running howeve...


python - Turn file into list

I have a file.txt like this: Hello Bye Good and I want to turn it into s = ["Hello", "Bye", "Good",] Thanks for helping


How to turn text file into python list form

I'm trying to turn a list of numbers in a text file into python list form. For example, I want to make 1 2 3 4 5 into [1,2,3,4,5] I found something that almost worked in another post using sed. sed '1s/^/[/;$!s/$/,/;$s/$/]/' file but this didn't remove the new line after every number. How can I modify this sed command to ...


python - What's the best Django search app?


How can I use a DLL file from Python?

What is the easiest way to use a DLL file from within Python? Specifically, how can this be done without writing any additional wrapper C++ code to expose the functionality to Python? Native Python functionality is strongly preferred over using a third-party library.


python - PubSub lib for c#

Is there a c# library which provides similar functionality to the Python PubSub library? I think it's kind of an Observer Pattern which allows me to subscribe for messages of a given topic instead of using events.


python - What is the best way to copy a list?

This question already has answers here:


python - Possible Google Riddle?

My friend was given this free google website optimizer tshirt and came to me to try and figure out what the front logo meant. t-shirt So, I have a couple of guesses as to what it means, but I was just wondering if there is something more. My first guess is that eac...


How do you check whether a python method is bound or not?

Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?


ssh - How to scp in Python?

What's the most pythonic way to scp a file in Python? The only route I'm aware of is os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) ) which is a hack, and which doesn't work outside Linux-like systems, and which needs help from the Pexpect module to avoid password prompts unless you already have passwordless SSH set up to the remote host. I'm aware of Twisted'...


python - How do I create a new signal in pygtk

I've created a python object, but I want to send signals on it. I made it inherit from gobject.GObject, but there doesn't seem to be any way to create a new signal on my object.


python - What do I need to import to gain access to my models?

I'd like to run a script to populate my database. I'd like to access it through the Django database API. The only problem is that I don't know what I would need to import to gain access to this. How can this be achieved?


python - How do I edit and delete data in Django?

I am using django 1.0 and I have created my models using the example in the Django book. I am able to perform the basic function of adding data; now I need a way of retrieving that data, loading it into a form (change_form?! or something), EDIT it and save it back to the DB. Secondly how do I DELETE the data that's in the DB? i.e. search, select and then delete! Please show me an example of the code ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top