Bbc news api example
The BBC News API is a free API that provides access to a vast collection of news articles, videos, and other multimedia content from the BBC. Here's an example of how to use the API:
API Endpoint
The BBC News API endpoint is http://feeds.bbci.co.uk/news/rss.xml
. You can also use the https
protocol instead of http
.
API Parameters
The API accepts several parameters to filter the results. Here are some examples:
category
: specifies the category of news articles to retrieve (e.g.,world
,politics
,business
, etc.)date
: specifies the date range for the news articles (e.g.,2022-01-01
to2022-01-31
)q
: specifies a search query to retrieve news articles that match the query
Example Request
Here's an example request to retrieve the top 10 news articles from the BBC News website:
http://feeds.bbci.co.uk/news/rss.xml?format=atom
This request retrieves the top 10 news articles in Atom format.
Example Response
The response will be an XML file containing the news articles. Here's an example response:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>BBC News</title>
<link href="http://feeds.bbci.co.uk/news/rss.xml" rel="self"/>
<link href="http://www.bbc.co.uk/news" rel="alternate"/>
<updated>2022-01-01T12:00:00Z</updated>
<entry>
<title>Breaking: UK economy shrinks by 0.1%</title>
<link href="http://www.bbc.co.uk/news/business-61234567" rel="alternate"/>
<published>2022-01-01T10:00:00Z</published>
<updated>2022-01-01T12:00:00Z</updated>
<summary>...</summary>
</entry>
<entry>
<title>UK weather: Snow and ice warnings issued</title>
<link href="http://www.bbc.co.uk/news/weather-61234568" rel="alternate"/>
<published>2022-01-01T09:00:00Z</published>
<updated>2022-01-01T12:00:00Z</updated>
<summary>...</summary>
</entry>
...
</feed>
Parsing the Response
To parse the response, you can use an XML parser library in your programming language of choice. For example, in Python, you can use the xml.etree.ElementTree
module:
import xml.etree.ElementTree as ET
response = requests.get('http://feeds.bbci.co.uk/news/rss.xml?format=atom')
root = ET.fromstring(response.content)
for entry in root.findall('.//entry'):
title = entry.find('title').text
link = entry.find('link').get('href')
published = entry.find('published').text
updated = entry.find('updated').text
summary = entry.find('summary').text
print(f"Title: {title}")
print(f"Link: {link}")
print(f"Published: {published}")
print(f"Updated: {updated}")
print(f"Summary: {summary}")
print()
This code retrieves the top 10 news articles and prints their title, link, published date, updated date, and summary.
Note
The BBC News API has usage limits and requires attribution. Be sure to review the API terms and conditions before using the API in your application.