0

我想用 R STudio 从 Guardian api 获取所有关于基因组编辑的文章。我尝试了一些代码并得到了几个错误。

data <- fromJSON("https://content.guardianapis.com/search?q=genome%20editing&show- 
blocks=body&&api-key=myapikey&page-size=200")

number_calls <- data$response$pages
api_calls <- paste("https://content.guardianapis.com/search?q=genome%20editing&show- 
blocks=body&api-key=myapikey&page- 
size=200page=", as.character(c(1:number_calls)), sep = "")

DatafromCall <- function(x) {
    data <- fromJSON(x)  
    body <- data$response$results$blocks$body
    textContent <- vector()
    for (i in seq_along(body)) {
    textContent[i] <- ifelse(is.null(body[[i]]$bodyTextSummary), NA, 
    body[[i]]$bodyTextSummary)
 }  
} 

for (i in 1:number_calls) {
all.data <- rbind(DataframeFromCall(api_calls[i]))

}

我在 open.connection(con, "rb") 中遇到错误:HTTP 错误 400

library(guardianapi)
Genomeediting <- gu_content(query= "genome%20editing")

我在 open.connection(con, "rb") 中遇到错误:HTTP 错误 403

有什么建议么?

4

2 回答 2

1

我使用 R 访问 API 的经验是,特定错误意味着地址中有错字。我没有使用 Guardian 的 API 访问它,所以我无法确定您的错误在哪里。

于 2021-01-25T22:21:53.500 回答
0

我得到了以下代码的解决方案:

library(jsonlite)
library(tidyverse)
library(httr)
library(stringr)

# APIs 
url1 <- paste("https://content.guardianapis.com/search?api- 
key=c3c4b8ff-bf4b-473f-832f-0440850a95be", sep = "", collapse="") 
query1 <- "&q=genome%20editing&show-blocks=body&page-size=1&page=1"
query2 <- "&q=genome%20editing&show-blocks=body&page- 
size=100&page="

### How many API calls
firstCall <- GET(paste(url1, query1, sep = "")) %>% content()
number_calls <- ceiling(firstCall$response$pages / 100)
api_calls <- paste(url1, query2, as.character(c(1:number_calls)), 
sep = "")

DataFrameFromCall <- function(x) {
  data <- fromJSON(x)  
  body <- data$response$results$blocks$body
  textContent <- vector()
  for (i in seq_along(body)) {
    textContent[i] <- ifelse(is.null(body[[i]]$bodyTextSummary), 
    NA, body[[i]]$bodyTextSummary)
  }
  TestData <- data.frame(textContent)
  return(TestData)
} 

all.data <- data.frame(textContent=character(),
                   stringsAsFactors=FALSE)

for (i in 1:number_calls) {
  all.data <- rbind(all.data, DatafromCall(api_calls[i]))
}

tibbleData <- as_tibble(all.data)

write.table(tibbleData, "GuardianGenomeEditing.txt", append = 
FALSE, sep = " ", dec = ".", row.names = TRUE, col.names = TRUE)
于 2020-03-11T15:16:51.003 回答