Search code examples
rweb-scrapinghttrfeedjsonlite

How to pull data from News River API into R


Trying to pull data from the NewsRiver API into R. Specifically would like to convert the json provided into a dataframe for further analysis. I would also like to be able to input my own search terms and domain I would like to search from as variables.

https://newsriver.io/


Solution

  • library(httr)
    library(jsonlite)
    
    set_config(config(ssl_verifypeer = 0L))
    
    search_1 <- "Amazon"
    search_2 <- "World Domination"
    website <- "bloomberg.com"
    
    
    url <- sprintf('https://api.newsriver.io/v2/search?query=text%%3A%s%%20OR%%20text%%3A%s%%20OR%%20website.domainName%%3A%s%%20OR%%20language%%3AEN&sortBy=_score&sortOrder=DESC&limit=100', search_1, search_2, website)
    api_key <- "mykey"
    
    news <- GET(url, add_headers(Authorization = paste(api_key, sep = "")))
    news_txt <- content(news, as = "text")
    news_china_df <- fromJSON(news_txt)