3
\$\begingroup\$

Trying to return the below values from an RSS feed. I have sorted out the RSS feed side of things but I am not sure if what I am doing is the best way to extract the data that I need. I have looked at beautifulsoup and regex's, re.search but not sure what is the best way to do this.

Values that I need; (both of these values change daily obviously)

4 Nov 2013

LOW-MODERATE

This is the cut down version of the full body of text that has the data i need to get from rss feed;

<p>Fire Danger Ratings<br />Bureau of Meteorology forecast issued at: Mon, 4 Nov 2013 05:30 AM</p>

<p>Central: LOW-MODERATE</p>

My code to extract the rss feed and data at the moment;

import feedparser #https://wiki.python.org/moin/RssLibraries

cfa_rss_url = "http://www.cfa.vic.gov.au/restrictions/central-firedistrict_rss.xml"

d = feedparser.parse( cfa_rss_url )

#get the data from the first item only, as it is only updated daily.
data = d.entries[0].description
print (data)
print('---------')

getdate = re.compile('forecast issued at: (.*?)</p>')
getrating = re.compile('<p>Central: (.*?)</p>')

m = getdate.search(data)
n = getrating.search(data)

print(m.group(1))
print(n.group(1))

This is the FULL data that is returned;

 <p>Total Fire Ban Status<br />Today, Mon, 4 Nov 2013 is <strong>not</strong> currently a day of Total Fire Ban in the <strong>Central (includes Melbourne and Geelong)</strong> fire district.</p><p>Fire Danger Ratings<br />Bureau of Meteorology forecast issued at: Mon, 4 Nov 2013 05:30 AM</p><p>Central: LOW-MODERATE</p></p><img alt="" border="0" src="http://www.cfa.vic.gov.au/images/fdr/central/low-moderate.gif" /></p><p><img alt="" height="22" src="http://www.cfa.vic.gov.au/images/fdr/tfb_icon_msg.gif" width="22" /> Displays when Total Fire Ban in force<br /><span><a href="http://www.cfa.vic.gov.au/warnings-restrictions/restrictions-during-the-fire-danger-period">Restrictions may apply</a></span></p>

---------
Mon, 4 Nov 2013 05:30 AM
LOW-MODERATE

As you can see my code returns the correct values atm but is this the most efficient way of doing this or should I be using another command?

\$\endgroup\$

migrated from stackoverflow.com Nov 14 '13 at 12:36

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ And.. what's the question? What do you want us to find? \$\endgroup\$ – aIKid Nov 4 '13 at 5:08
  • \$\begingroup\$ Am I using the best way to return the values that I require? the format of the rss feed doesn't change, just those few values. \$\endgroup\$ – Matt. Nov 4 '13 at 5:17
  • 1
    \$\begingroup\$ I think that's a fine way. \$\endgroup\$ – aIKid Nov 4 '13 at 5:18
  • \$\begingroup\$ @alKid Thx for that. \$\endgroup\$ – Matt. Nov 4 '13 at 5:32
  • \$\begingroup\$ @burhan, didn't know of that site, will check it out. \$\endgroup\$ – Matt. Nov 4 '13 at 5:33
2
\$\begingroup\$

You can do the whole thing with a single regexp:

import re

data = """
<p>Fire Danger Ratings
<br />Bureau of Meteorology 
forecast issued at: Mon, 4 Nov 2013 05:30 AM</p>

<p>Central: LOW-MODERATE</p>         
"""

rgx = re.compile("(forecast issued at: |<p>Central: )(.*?)</p>")
results = rgx.findall(data)
print results[0][1]
print results[1][1]

Output:

$ python rss_parse.py
Mon, 4 Nov 2013 05:30 AM
LOW-MODERATE

If this only runs a time or two a day, pre-compiling the regex is not important.

...
results = re.findall("(forecast issued at: |<p>Central: )(.*?)</p>", data)
print results[0][1]
print results[1][1]

We can simplify how the result is handled:

...
[when, rating] = [x[1] for x in
                  re.findall("(forecast issued at: |<p>Central: )(.*?)</p>",
                             data)]
print("%s\n%s" % (when, rating))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy