Displaying the titles of an RSS feed: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang="python"> #!/usr/bin/env python from optparse import OptionParser parser = OptionParser() parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Update: See [[argparse]]
<source lang="python">
<source lang="python">
#!/usr/bin/env python
#!/usr/bin/env python

Latest revision as of 14:38, 26 October 2011

Update: See argparse

#!/usr/bin/env python

from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co.uk/news/rss.xml", help="the url to read from")
parser.add_option("-n", "--numlines", type="int", dest="num", default=1000, help="how many lines to display")
(options, args) = parser.parse_args()

import feedparser
feed = feedparser.parse(options.url)
for e in feed.entries[:options.num]:
    print e.title.encode("utf-8")
#!/usr/bin/env python

from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co.uk/news/rss.xml", help="the url to read from")
parser.add_option("-n", "--numlines", type="int", dest="num", default=1000, help="how many lines to display")
(options, args) = parser.parse_args()

import feedparser
feed = feedparser.parse(options.url)
for e in feed.entries[:options.num]:
    print e.title.encode("utf-8")