A news and update api html
Here is an example of an HTML page that displays a news and update API:
index.html
<!DOCTYPE html>
<html>
<head>
<title>News and Updates API</title>
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h1>News and Updates API</h1>
<ul id="news-list"></ul>
<script src="script.js"></script>
</body>
</html>
script.js
fetch('https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_API_KEY')
.then(response => response.json())
.then(data => {
const newsList = document.getElementById('news-list');
data.articles.forEach(article => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<h2>${article.title}</h2>
<p>${article.description}</p>
<p><a rel="nofollow" target="_blank" href="${article.url}">Read more</a></p>
`;
newsList.appendChild(listItem);
});
})
.catch(error => console.error(error));
Note: Replace YOUR_API_KEY
with your actual API key from NewsAPI.org.
This HTML page uses the Fetch API to make a GET request to the NewsAPI.org API, which returns a JSON response containing a list of news articles. The script then parses the JSON response and creates a list of news items, which are displayed on the page.
Here's a breakdown of the script:
fetch
makes a GET request to the NewsAPI.org API with thecountry
parameter set tous
and theapiKey
parameter set to your actual API key.- The
then
method is used to handle the response data. In this case, we're usingresponse.json()
to parse the JSON response. - The
then
method is called again with the parsed JSON data. We're usingdata.articles
to access the list of news articles. - We loop through the list of articles and create a new
<li>
element for each one. - We set the innerHTML of each
<li>
element to a string that includes the article title, description, and a link to read more. - We append each
<li>
element to the<ul>
element with the IDnews-list
. - Finally, we catch any errors that may occur during the request and log them to the console.
This is just a basic example, and you can customize the HTML and CSS to fit your needs. You can also add more features, such as filtering or sorting the news articles, or displaying additional information like images or videos.