JSON 应用于 R 中的数据帧



我在一个网站上使用了以下内容,它返回了一个完美的结果:

寻找关键字:Emaar 粘贴在查询末尾:

library(httr)
library(jsonlite)

query<-"https://www.googleapis.com/customsearch/v1?key=AIzaSyA0KdZHRkAjmoxKL14eEXp2vnI4Yg_po38&cx=006431301429107149113:as7yqcm2qc8&q=Emaar"
result11 <- content(GET(query))
print(result11)
result11_JSON <- toJSON(result11)
result11_JSON <- fromJSON(result11_JSON)
result11_df <- as.data.frame(result11_JSON)

现在我想在包含关键字的 data.frame 上应用相同的函数:

所以我做了下面的测试.csv文件:

Company Name
[1]  ADES International Holding Ltd
[2]  Emirates REIT (CEIC) Limited
[3]  POLARCUS LIMITED

称之为测试网站提取.csv

使用的代码:

test_companies <- read.csv("... \Testing Website Extraction.csv")
#removing space and adding "+" sign then pasting query before it (query already has my unique google key and search engine ID
test_companies$plus <- gsub(" ", "+", test_companies$Company.Name)

query <- "https://www.googleapis.com/customsearch/v1?key=AIzaSyCmD6FRaonSmZWrjwX6JJgYMfDSwlR1z0Y&cx=006431301429107149113:as7yqcm2qc8&q="
test_companies$plus <- paste0(query, test_companies$plus)
a <- test_companies$plus
length(a)
function_webs_search <- function(web_search) {content(GET(web_search))}

result <- lapply(as.character(a), function_webs_search)

此处的结果显示了长度为 3(3 个搜索词(的列表,每个词中的子列表包含:url(列表[2](、查询(列表[2](、...项目(list[10](,并且每个搜索词的这些项目都是相同的(单独的长度相同(,我在这里的问题是应用代码的其余部分

#when i run:
result_JSON <- toJSON(result)
result_JSON <- as.list(fromJSON(result_JSON))

我得到一个包含子列表的 6 个列表的列表

并且将其放入一个整洁的数据帧中,其中结果彼此列出(而不是单独列出(被证明是困难的

另请注意,我尝试从"结果"列表中获取,该列表中有 3 个单独的列表,每个列表都是单独的,但是如果我有更长的关键字列表,则需要大量的体力劳动

预期的最终结果应包括 37 个变量的 30 个观测值(对于每个搜索词,37 个变量的 10 个观测值,并且所有观测值都彼此重叠。

我尝试过不成功的事情:

These work to flatten the list:
#do.call(c , result)
#all.equal(listofvectors, res, check.attributes = FALSE)
#unlist(result, recursive = FALSE)
# for (i in 1:length(result))  {listofvectors <- c(listofvectors, result[[i]])}
#rbind()
#rbind.fill()

即使在扁平化之后,我也不知道如何将它们组织成整洁的最终输出,供非 R 用户与之交互。

这里的任何帮助将不胜感激,

我在这里以防我的问题有什么不清楚的地方,

总是很高兴了解更多有关的信息,所以请耐心等待,因为我刚刚开始赶上。

万事如意,提前感谢!

基本上我所做的只是从数据帧列表中提取我需要的列,下面是最终代码:

library(httr)
library(jsonlite)
library(tidyr)
library(stringr)
library(purrr)
library(plyr)

test_companies <- read.csv("c:\users\... Companies Without Websites List.csv")
test_companies$plus <- gsub(" ", "+", test_companies$Company.Name)

query <- "https://www.googleapis.com/customsearch/v1?key=AIzaSyCmD6FRaonSmZWrjwX6JJgYMfDSwlR1z0Y&cx=006431301429107149113:as7yqcm2qc8&q="
test_companies$plus <- paste0(query, test_companies$plus)
a <- test_companies$plus
length(a)
function_webs_search <- function(web_search) {content(GET(web_search))}

result <- lapply(as.character(a), function_webs_search)
function_toJSONall <- function(all) {toJSON(all)}
a <- lapply(result, function_toJSONall)

function_fromJSONall <- function(all) {fromJSON(all)}
b <- lapply(a, function_fromJSONall)

function_dataframe <- function(all) {as.data.frame(all)}
c <- lapply(b, function_dataframe)
function_column <- function(all) {all[ ,15:30]}
result_final <- lapply(c, function_column)
results_df <- rbind.fill(c[])

最新更新