Reading news feed from BBC

If you are developing a social networking website with news and stories, you may be interested to present the news from various world news agencies, such as BBC and Reuters. Let us try to read news from BBC via a Python script.

Getting ready

This recipe relies on Python's third-party feedparser library. You can install this by running the following command:

$ pip install feedparser

Or

$ easy_install feedparser

How to do it...

First, we collect the BBC's news feed URL from the BBC website. This URL can be used as a template to search news on various types, such as world, UK, health, business, and technology. So, we can take the type of news to display as user input. Then, we depend on the read_news() function, which will fetch the news from the BBC.

Listing 6.6 explains how to read news feed from the BBC, as shown in the following code:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 6
# This program is optimized for Python 2.7. 
# It may run on any other version with/without modifications.


from datetime import datetime
import feedparser 
BBC_FEED_URL = 'http://feeds.bbci.co.uk/news/%s/rss.xml'

def read_news(feed_url):
  try:
    data = feedparser.parse(feed_url)
  except Exception, e:
    print "Got error: %s" %str(e)

  for entry in data.entries:
    print(entry.title)
    print(entry.link)
    print(entry.description)
    print("
") 

if __name__ == '__main__':
  print "==== Reading technology news feed from bbc.co.uk 
(%s)====" %datetime.today()

  print "Enter the type of news feed: "
  print "Available options are: world, uk, health, sci-tech, 
business, technology"
  type = raw_input("News feed type:")
  read_news(BBC_FEED_URL %type)
  print "==== End of BBC news feed ====="

Running this script will show you the available news categories. If we choose technology as the category, you can get the latest news on technology, as shown in the following command:

$ python 6_6_read_bbc_news_feed.py 
==== Reading technology news feed from bbc.co.uk (2013-08-20 19:02:33.940014)==== 
Enter the type of news feed:
Available options are: world, uk, health, sci-tech, business, technology 
News feed type:technology 
Xbox One courts indie developers 
http://www.bbc.co.uk/news/technology-23765453#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
Microsoft is to give away free Xbox One development kits to encourage independent developers to self-publish games for its forthcoming console. 

Fast in-flight wi-fi by early 2014 
http://www.bbc.co.uk/news/technology-23768536#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
Passengers on planes, trains and ships may soon be able to take advantage of high-speed wi-fi connections, says Ofcom. 

Anonymous 'hacks council website' 
http://www.bbc.co.uk/news/uk-england-surrey-23772635#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
A Surrey council blames hackers Anonymous after references to a Guardian journalist's partner detained at Heathrow Airport appear on its website. 

Amazon.com website goes offline 
http://www.bbc.co.uk/news/technology-23762526#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
Amazon's US website goes offline for about half an hour, the latest high-profile internet firm to face such a problem in recent days. 

[TRUNCATED]

How it works...

In this recipe, the read_news() function relies on Python's third-party module feedparser. The feedparser module's parser() method returns the feed data in a structured fashion.

In this recipe, the parser() method parses the given feed URL. This URL is constructed from BBC_FEED_URL and user input.

After some valid feed data is obtained by calling parse(), the contents of the data is then printed, such as title, link, and description, of each feed entry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.152.58