In this commercial world, there are a lot of Newspaper publishers and websites available. There are a myriad number of articles about a wide variety of topics. What if you can search all the articles yourself just by entering a keyword and can design them too. Doesn’t it sound interesting!! In this article, let’s discuss how to create a NEWS search based on keywords using open-source NEWS API and python.
What is an open-source API??
Open-source API/public API (Application Programming Interface) is an open public accessible API, which provides the interface between the program and the application.
NEWS API:
An open-source NEWS API is a URL which on requested gives required data in JSON format containing the following-
- Status of request
- Total results available
- Array of articles
{
“status”: “ok”,
“totalResults”: 38,
“articles”: []
}
And each element of the array(articles) is again a JSON format which contains
- Source (JSON format with contents ID and NULL)
- Title
- Author
- Description
- URL
- URL to image
- Published At
- Content
{
“source”: {
“Id”: “ID“,
“name”: “NAME”
}
“author”: “xxxxxxxxxx”,
“title”: “How to use NEWS API?”,
“description”: “NEWS API is an open-source API service that can be used by anybody”,
“url”: “https://newsapi.org/”,
“urlToImage”: “https://newsapi.org/”,
“publishedAt”: “2020-11-20T09:20:19Z”,
“content”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
}
This open-source API is developed and maintained by the NEWS API
How to generate NEWS API?
- Firstly, open the NEWS API.
- Then click on the GET API KEY.
- Now provide the mentioned details for creating an account with the NEWS API.
- First name
- Password
- Choose whether you are an individual or a group
- Confirm you are not a robot
- Agree to their terms
- After Signing in you will have 5 types of API available.
- All articles based on key-word of recent Month sorted by recently published.
- Top Business Headlines in the U.S right now.
- All articles mentioning a key-word sorted by popular publisher.
- Top articles from TechCrunch at present.
- All articles published by Wall street journal in the last 6 months.
- After signing in, you will have the link shown in the image above with the API. Access your API key in the profile section.
- So the API is generated successfully.
- In this article, we will discuss the first type of API, and how to get all the URLs of NEWS related to the keyword provided.
SOURCE CODE:
The code is written using Python and a detailed explanation of the same is given below.
- First, make sure the ‘requests’ and ‘JSON’ libraries are installed.
- Then, import requests and JSON libraries.
- Now take the input key-word using input() method.
- Copy the API from NEWS API
http://newsapi.org/v2/everything?q=bitcoin&from=2020-10-20&sortBy=publishedAt&apiKey=API_KEY
(If you are signed in, then the API_KEY will be automatically replaced)
- Replace the topic name with your input in API using the Concatenation operator(‘+’).
- Make a web request using requests class.
- request(“GET” , URL) – Method to make a GET from mentioned URL.
- ,text – attribute to the above method to get a text as string from the URL.
- Convert the string into a JSON format
- Json.loads(‘str’) – method to convert string to json. The string should be properly formatted as per the JSON format beforehand.
- Iterate over ‘articles’ array to get the URL of each article and print the required URL in each loop(refer JSON structure at the beginning of the article for a better understanding).
- P [‘articles’][i][‘url’]
- The whole code looks like this-
The coding is done. Now let’s run it. While executing the code, you must ensure that the system is connected to the internet.
The sample output looks as shown:
CONCLUSION:
Open-source API is very useful in the IoT industry. These APIs are changing the way applications interact with each other and are going to be used widely in the days to come. In this article, we’ve learnt about the open-source API, NEWS API, step by step manner to generate the API and Detailed explanation of the source code used.
so, Try this simple program and you can play around with the code using other open-source APIs like weather API, Youtube API, Instagram API e.t.c. Have fun creating some exciting tasks.
Written by: Batta Pruthvi
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs