Export News into a CSV with Python
In this tutorial, you’ll learn how to extract data from NewsCatcher News API and write it into a CSV file.
Requirements
Python 3
Preinstalled packages:
You can copy paste packages name and version and put them into separated file "requirements.txt". Then, using terminal, install with a signle command
Steps to accomplish
Prepare the environment
Make an API call
Extract all found news articles
Export data to CSV file
From Python Dictionary
From Pandas table
1. Prepare the environment
This step consists of:
import packages
set environmental variables (API Key, URL of the News API, etc)
define a correct work folder
The following code illustrates the above steps:
Receive your API key by registering at app.newscatcherapi.com
2. Make an API call
Let's take it easy and try to make a single call. For example, we would like to look for all mentions of 3 popular cryptocurrencies Bitcoin, Ethereum, and Dogecoin.
In order to make a call, we need to set headers and parameters. In parameters, I am also filtering on articles in English as well as narrow down the search top 10 000 the most trustful news sources based on rank variable . Default timeperiod is set to 1 week, so no need to define this parameter.
If the status_code is not 200, the error message should give you a clear idea of what was wrong
Here are the results that we received:
As you can see, we found 253 articles mentioning all three popular cryptocurrencies in one article. Another parameter worth looking at is "total_pages". It shows how many API calls you will have to make in order to extract all found news articles. We will use it later in the guide. Besides, you can explore further by looking at each article separately. All of them are stored in "articles" JSON Key.
If you want to know more about all response data you get, go check our Search Endpoint page
(Optional) Visualize data with pandas package
To be able to look at all 100 articles at the same time, let's create a pandas table from the "articles" Key.
Now, you can have a closer look at the first 100 articles, before extracting the remaining.
3. Extract All Found News Articles
At this stage, we are already confident that an API call returns expected results. The next step is to extract all found news articles using "total_pages" value.
One thing to keep in mind is that I am using Free Trial API Key, where the frequency of API calls is limited to 1 call/second. So, to not be penalized for overuse, I make my code wait for one second between each call.
In summary, we iterate through all available pages, extract news articles and store them in one variable called "all_news_article". We also add used parameters to each article, so when exploring you can see where it comes from. You can always delete this part of code if you do not want to have this information in your CSV file.
(Optional) Having multiple queries
Imagine that you want to extract news data from multiple queries at one time. So, instead of searching for articles where all 3 popular cryptocurrencies are mentioned, you would like to look for each of them separately and adding "business" as a topic. In this case, you will have multiple parameters and you will have to add one more iteration.
Here is how the params variable looks like:
In the code, we added one more iteration and put "separated_param" inside requests.get function.
One more important thing is to deduplicate results. Right now we extract articles from 3 different queries. But, as we saw before, there can be mentions of all 3 of them in the same article. So different queries can bring the same articles. That is why we have "_id" value generated for each article. ID is created by decoding both title and clean_url (Web Domain Name of the News Source).
Here is how you can deduplicate results in Python:
4. Export Data to CSV file
4.1 From Python dictionary
You can save a file directly from the dictionary generated previously:
4.2 From Pandas table
Or create a Pandas table, check the results and then generate a CSV:
Last updated