首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用GTmetrix REST v2.0

在R中使用GTmetrix REST v2.0
EN

Stack Overflow用户
提问于 2021-10-24 15:44:22
回答 1查看 170关注 0票数 2

我正在尝试集成使用GTmetrix的某些网站的性能测试。使用API,我可以使用Microsoft中的SEO连接器工具运行测试并提取结果。然而,它使用的xml与较早版本的API,一些新的测试是不可用的。最新版本是2.0

xml的链接在这里:GTmetrix XMLforAPI0.1

我试过使用httr和jsonlite库。但是,我不知道如何使用API进行身份验证,运行测试并提取结果。

API的文档可在API文档上获得。

代码语言:javascript
复制
library(httr)
library(jsonlite)

url  <- "https://www.berkeley.edu" # URL to be tested
location <- 1 # testing Location
browser <- 3 # Browser to be used for testing
res  <- GET("https://gtmetrix.com/api/gtmetrix-openapi-v2.0.json")
data <- fromJSON(rawToChar(res$content))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-06 20:47:46

更新2021-11-08:

我创建了一个小型库,通过R与GTmetrix对话。这里有一些基本的健全检查,但很明显,这仍在进行中,而且还存在一些(潜在的关键)错误。不过,你可以随便看看。会喜欢一些反馈的。

代码语言:javascript
复制
# Install and load library.
devtools::install_github("RomanAbashin/rgtmx")
library(rgtmx)

更新2021-11-12:它现在可以在CRAN上使用。:-)

代码语言:javascript
复制
# Install and load library.
install_packages("rgtmx")
library(rgtmx)

开始测试(并获得结果)

代码语言:javascript
复制
# Minimal example #1.
# Returns the final report after checking test status roughly every 3 seconds. 
result <- start_test("google.com", "[API_KEY]")

这将启动一个测试,并等待生成报告,并以data.frame的形式返回结果。或者,您只需通过参数wait_for_completion = FALSE返回测试ID和其他元数据即可。

代码语言:javascript
复制
# Minimal example #2.
# Returns just the test ID and some meta data.
result <- start_test("google.com", "[API_KEY]", wait_for_completion = FALSE)

其他可选参数:locationbrowserreportretentionhttpauth_usernamehttpauth_passwordadblockcookiesvideostop_onloadthrottleallow_urlblock_urldnssimulate_deviceuser_agentbrowser_widthbrowser_heightd23、browser_rotate。

显示可用浏览器

代码语言:javascript
复制
show_available_browsers("[API_KEY]")

显示可用位置

代码语言:javascript
复制
show_available_locations("[API_KEY]")

获得特定测试

代码语言:javascript
复制
get_test("[TEST_ID]", "[API_KEY]")

得到具体的报告

代码语言:javascript
复制
get_report("[REPORT_ID]", "[API_KEY]")

做所有的测试

代码语言:javascript
复制
get_all_tests("[API_KEY]")

获取帐户状态

代码语言:javascript
复制
get_account_status("[API_KEY]")

原来的答案:

实际上,很简单:

0。设定测试参数。

代码语言:javascript
复制
# Your api key from the GTmetrix console.
api_key <- "[Key]"

# All attributes except URL are optional, and the availability
# of certain options may depend on the tier of your account.

# URL to test.
url <- "https://www.worldwildlife.org/"
# Testing location ID.
location_id <- 1
# Browser ID.
browser_id <- 3

1.开始测试

代码语言:javascript
复制
res_test_start  <- httr::POST(
    url = "https://gtmetrix.com/api/2.0/tests",
    httr::authenticate(api_key, ""),
    httr::content_type("application/vnd.api+json"),
    body = jsonlite::toJSON(
        list(
            "data" = list(
                "type" = "test",
                "attributes" = list(
                    "url" = url,
                    # Optional attributes go here.
                    "location" = location_id,
                    "browser" = browser_id
                )
            )
        ),
        auto_unbox = TRUE
    ),
    encode = "raw"
)

2.获取测试ID

代码语言:javascript
复制
test_id <- jsonlite::fromJSON(rawToChar(res_test_start$content))$data$id

3.获取报告ID

代码语言:javascript
复制
# Wait a bit, as generating the report can take some time.
res_test_status <- httr::GET(
    url = paste0("https://gtmetrix.com/api/2.0/tests/", test_id),
    httr::authenticate(api_key, ""),
    httr::content_type("application/vnd.api+json")
)

# If this returns the test ID, the report is not ready, yet.
report_id <- jsonlite::fromJSON(rawToChar(res_test_status$content))$data$id

4.取得报告

代码语言:javascript
复制
res_report <- httr::GET(
    url = paste0("https://gtmetrix.com/api/2.0/reports/", report_id),
    httr::authenticate(api_key, ""),
    httr::content_type("application/vnd.api+json")
)

# The report is a nested list with the results as you know them from GTmetrix.
report <- jsonlite::fromJSON(rawToChar(res_report$content))$data

我有点想为此做点什么,因为似乎没有R库.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69698336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档