在r中解开列表中的列表中的列表,
我遇到了一些困难,可以解开Google页面速度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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法弄清楚了。可能不是最好的解决方案或最清洁的解决方案,但它起作用。
希望这对与Google PageSpeed API和R一起工作的其他人有帮助。
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.