在r中解开列表中的列表中的列表,

发布于 2025-02-11 17:11:31 字数 945 浏览 4 评论 0原文

我遇到了一些困难,可以解开Goog​​le页面速​​度API响应中列表中的列表。

理想情况下,我只想将审核结果导出为CSV文件。因此,我可以比较客户网站的网站加载时间和性能。

''

library(httr)
library(tidyverse)
library(tidyr)

#URL to submit GET request to
url <- "https://www.googleapis.com/pagespeedonline/v5/runPagespeedurl=https://www.google.com/"


# GET request returned as list
raw_list <- url %>% 
httr::GET() %>% 
httr::content()


#turning the list into a dataframe 
df_pagespeed <- as.data.frame(do.call(rbind, raw_list))

#attempted unpack list in audit results with no luck
df_pagespeed <- tidyr::unnest(df_pagespeed, cols = audits)

# select only the audit results. 
df_pagespeed_final <- df_pagespeed[c(audits)]

#export to csv file
write.csv(df_pagespeed_final,"test-pagespeed.csv", row.names = FALSE)

''

理想情况下,我希望第二个数据帧(DF_PAGESPEED_FINAL)包含与PagesPeed审计结果相关的信息。有意义的见解,例如我的第一个智商,

希望能够清楚地了解某人。如果没有,请告诉我,我将修改这个问题。

感谢您的帮助。

I am having some difficulties unpacking what looks like a list within a list in Google's Page Speed API response.

Ideally, I want only audit results exported as a CSV file. So I can compare the website load times and performance of my client's website.

'''

library(httr)
library(tidyverse)
library(tidyr)

#URL to submit GET request to
url <- "https://www.googleapis.com/pagespeedonline/v5/runPagespeedurl=https://www.google.com/"


# GET request returned as list
raw_list <- url %>% 
httr::GET() %>% 
httr::content()


#turning the list into a dataframe 
df_pagespeed <- as.data.frame(do.call(rbind, raw_list))

#attempted unpack list in audit results with no luck
df_pagespeed <- tidyr::unnest(df_pagespeed, cols = audits)

# select only the audit results. 
df_pagespeed_final <- df_pagespeed[c(audits)]

#export to csv file
write.csv(df_pagespeed_final,"test-pagespeed.csv", row.names = FALSE)

'''

Ideally I want the second dataframe (df_pagespeed_final) to contain information related to pagespeed audit results. Meaningful insights like my first-contentful-paint

Hopefully that is clear enough for someone to understand. If not, please let me know and I will revise the question.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2025-02-18 17:11:32

我设法弄清楚了。可能不是最好的解决方案或最清洁的解决方案,但它起作用。

希望这对与Google PageSpeed API和R一起工作的其他人有帮助。


library(httr)
library(tidyverse)
library(tidyr)
library(purrr)
library(magrittr)
library(ggplot2)
library(reshape)

#URL to submit GET request to
url <- "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.google.com/"


# GET request returned as list
raw_list <- url %>% 
  httr::GET() %>% 
  httr::content()



#turning the list into a dataframe 
df_pagespeed <- as.data.frame(do.call(rbind, raw_list))

df_all_audit <- df_pagespeed["lighthouseResult", "audits"]

df_all_audit <- as.data.frame(do.call(rbind, df_all_audit))


#df for meaningful paint 
df_first_meaningful_paint <- df_all_audit["lighthouseResult", "first-meaningful-paint"]

df_first_meaningful_paint <- as.data.frame(do.call(rbind, df_first_meaningful_paint))

df_first_meaningful_paint <- df_first_meaningful_paint$numericValue[1]



#df for largest content paint
df_largest_content_paint <- df_all_audit["lighthouseResult", "largest-contentful-paint"]

df_largest_content_paint <- as.data.frame(do.call(rbind, df_largest_content_paint))

df_largest_content_paint <- df_largest_content_paint$numericValue[1]



#df for total-blocking time
df_total_blocking_time <- df_all_audit["lighthouseResult", "total-blocking-time"]

df_total_blocking_time <- as.data.frame(do.call(rbind, df_total_blocking_time))

df_total_blocking_time <- df_total_blocking_time$numericValue[1]


#df for total-blocking time
df_speed_index <- df_all_audit["lighthouseResult", "speed-index"]

df_speed_index <- as.data.frame(do.call(rbind, df_speed_index ))

df_speed_index  <- df_speed_index$numericValue[1]



#df for content paint 
df_first_content_paint <- df_all_audit["lighthouseResult", "first-contentful-paint"]

df_first_content_paint <- as.data.frame(do.call(rbind, df_first_content_paint))

df_first_content_paint <- df_first_content_paint$numericValue[1]


#df for cumulative layout shift 
df_cumulative_shift <- df_all_audit["lighthouseResult", "cumulative-layout-shift"]

df_cumulative_shift <- as.data.frame(do.call(rbind, df_cumulative_shift))

df_cumulative_shift <- df_cumulative_shift$numericValue[1]



#df for server response time
df_server_response_time <- df_all_audit["lighthouseResult", "server-response-time"]

df_server_response_time<- as.data.frame(do.call(rbind, df_server_response_time))

df_server_response_time <- df_server_response_time$numericValue[1]



now <- Sys.time()
time <- data.frame(now)


#put all data frames into list
df_list <- bind_cols(time, df_first_content_paint, df_first_meaningful_paint, df_largest_content_paint, df_total_blocking_time, df_speed_index, df_cumulative_shift, df_server_response_time)

# renaming columns in dataframe
names(df_list)[1] <- "time"
names(df_list)[2] <- "first_content_paint"
names(df_list)[3] <- "first_meaningful_paint"
names(df_list)[4] <- "largest_content_paint"
names(df_list)[5] <- "total_blocking_time"
names(df_list)[6] <- "speed_index"
names(df_list)[7] <- "cumulative_shift"
names(df_list)[8] <- "server_response_time"

#assigning data to df_pagespeed_new
df_pagespeed_new <- df_list

# loading in old pagespeed file
df_pagespeed_old <- read_csv("pagespeed.csv")

#adding additional row to df page speed 
total <- rbind(df_pagespeed_old, df_pagespeed_new)

#writing the new new dataframe (larger more rows to dataframe)
write.csv(total,"pagespeed.csv", row.names = FALSE)


#plotting the graph
p <- ggplot()+
  geom_line(data=total,aes(y=first_content_paint,x= time,colour="first_content_paint"),size=1 )+
  geom_line(data=total,aes(y=largest_content_paint,x= time,colour="largest_content_paint"),size=1 )+
  geom_line(data=total,aes(y=total_blocking_time,x= time,colour="total_blocking_time"),size=1 )+
  geom_line(data=total,aes(y=speed_index,x= time,colour="speed_index"),size=1 )+
  geom_line(data=total,aes(y=cumulative_shift,x= time,colour="cumulative_shift"),size=1 )+
  geom_line(data=total,aes(y=server_response_time,x= time,colour="server_response_time"),size=1) +
  scale_color_manual(name = "Speed Metrics", values = c("first_content_paint" = "#008080", "largest_content_paint" = "#58508d", "total_blocking_time" = "#bc5090", "speed_index" = "#ff6361", "cumulative_shift" = "#ffa600", "server_response_time" = "#003f5c")) +
  xlab("Time & Date") +
  scale_y_continuous("Loadtime (milliseconds)") + 
  labs(title="www.google.com Page Speed Metrics")+ 
  theme(plot.title=element_text(hjust=0.5))

p + theme_classic() # Classic theme

I managed to figure it out. Probably not the best solution or cleanest, but it works.

Hopefully this helps someone else out, who is working with the Google Pagespeed API and R.


library(httr)
library(tidyverse)
library(tidyr)
library(purrr)
library(magrittr)
library(ggplot2)
library(reshape)

#URL to submit GET request to
url <- "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://www.google.com/"


# GET request returned as list
raw_list <- url %>% 
  httr::GET() %>% 
  httr::content()



#turning the list into a dataframe 
df_pagespeed <- as.data.frame(do.call(rbind, raw_list))

df_all_audit <- df_pagespeed["lighthouseResult", "audits"]

df_all_audit <- as.data.frame(do.call(rbind, df_all_audit))


#df for meaningful paint 
df_first_meaningful_paint <- df_all_audit["lighthouseResult", "first-meaningful-paint"]

df_first_meaningful_paint <- as.data.frame(do.call(rbind, df_first_meaningful_paint))

df_first_meaningful_paint <- df_first_meaningful_paint$numericValue[1]



#df for largest content paint
df_largest_content_paint <- df_all_audit["lighthouseResult", "largest-contentful-paint"]

df_largest_content_paint <- as.data.frame(do.call(rbind, df_largest_content_paint))

df_largest_content_paint <- df_largest_content_paint$numericValue[1]



#df for total-blocking time
df_total_blocking_time <- df_all_audit["lighthouseResult", "total-blocking-time"]

df_total_blocking_time <- as.data.frame(do.call(rbind, df_total_blocking_time))

df_total_blocking_time <- df_total_blocking_time$numericValue[1]


#df for total-blocking time
df_speed_index <- df_all_audit["lighthouseResult", "speed-index"]

df_speed_index <- as.data.frame(do.call(rbind, df_speed_index ))

df_speed_index  <- df_speed_index$numericValue[1]



#df for content paint 
df_first_content_paint <- df_all_audit["lighthouseResult", "first-contentful-paint"]

df_first_content_paint <- as.data.frame(do.call(rbind, df_first_content_paint))

df_first_content_paint <- df_first_content_paint$numericValue[1]


#df for cumulative layout shift 
df_cumulative_shift <- df_all_audit["lighthouseResult", "cumulative-layout-shift"]

df_cumulative_shift <- as.data.frame(do.call(rbind, df_cumulative_shift))

df_cumulative_shift <- df_cumulative_shift$numericValue[1]



#df for server response time
df_server_response_time <- df_all_audit["lighthouseResult", "server-response-time"]

df_server_response_time<- as.data.frame(do.call(rbind, df_server_response_time))

df_server_response_time <- df_server_response_time$numericValue[1]



now <- Sys.time()
time <- data.frame(now)


#put all data frames into list
df_list <- bind_cols(time, df_first_content_paint, df_first_meaningful_paint, df_largest_content_paint, df_total_blocking_time, df_speed_index, df_cumulative_shift, df_server_response_time)

# renaming columns in dataframe
names(df_list)[1] <- "time"
names(df_list)[2] <- "first_content_paint"
names(df_list)[3] <- "first_meaningful_paint"
names(df_list)[4] <- "largest_content_paint"
names(df_list)[5] <- "total_blocking_time"
names(df_list)[6] <- "speed_index"
names(df_list)[7] <- "cumulative_shift"
names(df_list)[8] <- "server_response_time"

#assigning data to df_pagespeed_new
df_pagespeed_new <- df_list

# loading in old pagespeed file
df_pagespeed_old <- read_csv("pagespeed.csv")

#adding additional row to df page speed 
total <- rbind(df_pagespeed_old, df_pagespeed_new)

#writing the new new dataframe (larger more rows to dataframe)
write.csv(total,"pagespeed.csv", row.names = FALSE)


#plotting the graph
p <- ggplot()+
  geom_line(data=total,aes(y=first_content_paint,x= time,colour="first_content_paint"),size=1 )+
  geom_line(data=total,aes(y=largest_content_paint,x= time,colour="largest_content_paint"),size=1 )+
  geom_line(data=total,aes(y=total_blocking_time,x= time,colour="total_blocking_time"),size=1 )+
  geom_line(data=total,aes(y=speed_index,x= time,colour="speed_index"),size=1 )+
  geom_line(data=total,aes(y=cumulative_shift,x= time,colour="cumulative_shift"),size=1 )+
  geom_line(data=total,aes(y=server_response_time,x= time,colour="server_response_time"),size=1) +
  scale_color_manual(name = "Speed Metrics", values = c("first_content_paint" = "#008080", "largest_content_paint" = "#58508d", "total_blocking_time" = "#bc5090", "speed_index" = "#ff6361", "cumulative_shift" = "#ffa600", "server_response_time" = "#003f5c")) +
  xlab("Time & Date") +
  scale_y_continuous("Loadtime (milliseconds)") + 
  labs(title="www.google.com Page Speed Metrics")+ 
  theme(plot.title=element_text(hjust=0.5))

p + theme_classic() # Classic theme

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文