Get news from newsapi using R language
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
library (httr)
library (jsonlite)
=us&category=business&apiKey=<YOURAPIKEY>"
news = GET (url)
data = rawToChar (news$content)
jsondata = fromJSON (data)
print (jsondata$articles$title)
|
Make sure to replace <YOURAPIKEY> with your API key in url.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...