r - 将日期参数传递到 REST API 调用 - 使用 R-

标签 r url-encoding jsonlite

尝试从 REST API 中提取一些数据,但无法作为日期参数正确传递到字符串中。使用 sprintf,我成功地传递了搜索词和网站,但发现Date 却没有成功。

https://newsriver.io是有问题的 API

Function to grab data by one search term and one website

get_newsriver_content <- function(searcht,website,api_key){
url <- sprintf('https://api.newsriver.io/v2/search?query=text%%3A%s%%20OR%%20website.domainName%%3A%s%%20OR%%20language%%3AEN&sortBy=_score&sortOrder=DESC&limit=100',searcht, website)
news_get<- GET(url, add_headers(Authorization = paste(api_key, sep = "")))
news_txt <- content(news_get, as = "text", encoding = "UTF-8") 
news_df <- fromJSON(news_txt)
news_df$discoverDate <- as.Date(news_df$discoverDate)
news_df
}

问题已更新 - 我还想根据日期向量进行多个 API 调用。

最佳答案

这是我解决问题的方法

这确实是一个两步问题

  1. 弄清楚如何正确编码要插入到 Curl 调用中的查询
  2. 创建一个函数,该函数根据日期向量进行 API 调用并将其附加到数据框。

这是我的做法。

library(tidyverse)
library(jsonlite)
library(urltools)
library(httr)

# Function For Pulling by Date  
get_newsriver_bydate <- function(query, date_v){

#Being Kind to the free API - Shout out to Elia at Newsriver who has been ever patient
pb$tick()$print()
Sys.sleep(sample(seq(0.5, 2.5, 0.5), 1))

#This is where is used the URL encode package as suggested by quartin
url_base <- "https://api.newsriver.io/v2/search"
create_curl_call <- url_base %>% 
param_set("query",url_encode(query)) %>% 
param_set("sortBy", "_score") %>% 
param_set("sortOrder", "DESC") %>% 
param_set("limit", "100") 

#I had most of this before however I changed my output to a tibble
#more versatile to work with 

get_curl <- GET(create_curl_call, add_headers(Authorization = paste(api_key, sep = "")))
curl_to_json <- content(get_curl, as = "text", encoding = "UTF-8")
news_df <- fromJSON(curl_to_json, flatten = TRUE)
news_df$discoverDate <- as.Date(news_df$discoverDate)
as.tibble(news_df)
}

# Set Configration and Set API key
set_config(config(ssl_verifypeer = 0L))
api_key <- "mykey"

#Set my vector of Dates
dates1 <- seq(as.Date("2017-09-01"), as.Date("2017-10-01"), by = "days")

#Set up my progress bar
pb <- progress_estimated(length(dates1))

#Sprintf my query into a vector of queries based on date
query <- sprintf('text:"Canada" AND text:"Rocks" AND language:EN AND discoverDate:[%s TO %s]',dates1, dates1)

 #Run the query and be patient
news_df <- map_df(query, get_newsriver_bydate, .id = "query")

那么我的研究方法以及我如何解决这两个问题

  1. Quartin 给了我一个查找 urltools 包的建议 https://cran.rstudio.com/web/packages/urltools/index.html - 该软件包可帮助您对 URL 以及各种其他快速且矢量化的函数进行编码和解码。接下来我的问题是让我的查询正确,我只是查阅了 API 文档,我建议任何试图从 API 中提取数据的人都这样做。听起来可能很简单,但在发布我的问题之前我没有完整阅读它

  2. 创建函数时我使用了许多以前的答案来帮助构建它,但是下面的帖子帮助最大

API Query for loop 这篇文章帮助我使用进度条和 map 功能将所有内容放入一个数据框中。

很可能有更好的答案,但到目前为止这对我有用。

关于r - 将日期参数传递到 REST API 调用 - 使用 R-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543082/

相关文章:

javascript - JS encodeURIComponent 结果与 FORM 创建的不同

url - 为什么即使参数不是 URL 编码的,某些查询字符串也能工作?

r - 如何将一个大型、复杂、深度嵌套的 JSON 文件扁平化为多个 CSV 文件(链接标识符)

r - 将数据从 rhandsontable 对象转换为 Shiny 中的数据框

r - 使用 ggplot 和 R 用多边形裁剪轮廓

r - R 中具有不同变量组合的线性模型

javascript - Play 后端和 JavaScript 客户端之间的编码/解码 URL

json - 从一个目录导入多个json文件并附加数据

json - 将数据框转换为嵌套的 json 格式

r - 汇总分钟到小时的需求