AEA
AEA

Reputation: 223

Generating simple RSS feed from parsed data

Hello Stack Overflow I have some code that scrapes a website and parses small sections of data as below;

System MA
user id =  2084
username =  XYZ90
selection =  pnq
decimal =  6.000
Percentage =  19.1176470588 %

System NA
user id =  2086
username =  pron1
selection =  abc
decimal =  13.000
Percentage =  13.1147540984 %

System M
user id =  1664
username =  Chrisod
selection =  pleader
decimal =  15.000
Percentage =  16.091954023 %

The above is printed as a result of the following filters:

if (deciaml > 5 and percentage > 10:
    print "system", system_acn
    print "user id = ",user_id
    print "username = ",username
    print "selection = ",selection
    print "decimal = ",decimal_delim   
    print "percentage = ,percentage_calc2,"%"

This is all as a results of printing different parameters from a looping webscraper. What I want to be able to do is to is export each as an RSS. So the above would exist as three RSS posts, within the RSS feed. I have researched some modules such as django, this seems to be a toolkit for many things not specifically RSS, essentially what I am looking for the simplest and easiest RSS solution possible? Any advice or comments on suitable methodologies and or modules would be greatly appreciated. Kind regards AEA

Upvotes: 3

Views: 383

Answers (1)

foobarbecue
foobarbecue

Reputation: 7060

I noticed you have tagged your question as pertaining to Django. If you are going to build a Django application based on this problem, then the syndication framework is what you want to use. However, this isn't worth it unless you are planning to use other components of Django, such as the database stuff and / or the template language.

However, you asked for the "simplest and easiest" solution. I love Django and it only takes me minutes to set up an app so for me the easiest way to do your whole project would probably be to make a quick Django app. The simplest solution is probably to create the feed manually, shouldn't be that hard; something like:

inp="""System MA
user id =  2084
username =  XYZ90
selection =  pnq
decimal =  6.000
Percentage =  19.1176470588 %

System NA
user id =  2086
username =  pron1
selection =  abc
decimal =  13.000
Percentage =  13.1147540984 %

System M
user id =  1664
username =  Chrisod
selection =  pleader
decimal =  15.000
Percentage =  16.091954023 %"""

inp=inp.split('\n\n')

rss_start="""<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>Your title</title>
  <link>http://yoursite.com</link>
  <description>Your discription</description>
"""

rss_end="""</channel>

</rss> """

def description(item):
    return item

def title(item):
    return item.split('\n')[0]

def link(item):
    return 'http://mysite.com/' + item.split('\n')[0]

rss_items=[]
for counter, item in enumerate(inp):
    rss_items.append("""
  <item>
    <title>%s</title>
    <link>%s</link>
    <description>%s</description>
    <guid>counter</guid>
  </item>""" % (title(item),description(item),link(item)))

rss_output=rss_start+''.join(rss_items)+rss_end

You probably also want to add <pubDate> tags. And make sure your <guid>s are unique.

Note: rss template copied from w3schools.com

Upvotes: 6

Related Questions