Skip to content
Related Articles

Related Articles

Get news from newsapi using R language

View Discussion
Improve Article
Save Article
  • Last Updated : 18 Mar, 2022

In this article, we will learn how to create a Rscript to read the latest news. We will be using news API to get news and extract it using httr package in R Programming Language.

Modules Needed:

install.packages(“httr”)

install.packages(“jsonlite”)

Get news API:

To get your API key visit newsapi.org and create your account. 

Click on the Get API key to get your key.  Make sure you have saved your key.

We will be using httr GET() to make URL requests and store news data in a variable. Now we will need JSON data so we have to convert the news data into char format because GET() returns raw data so we will need to use rawToChar() and to convert char data in JSON format we will be using from JSON() built-in jsonlite package.

Implementation:

R




# importing packages
library(httr)
library(jsonlite)
  
# declaring url
=us&category=business&apiKey=<YOURAPIKEY>"
  
# making http request and storing it in 
# news variable
news = GET(url)
  
# converting raw data to character
data = rawToChar(news$content)
  
# converting character to json format
jsondata = fromJSON(data)
  
# printing news title
print(jsondata$articles$title)


Make sure to replace <YOURAPIKEY> with your API key in url.

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!