【问题标题】:Using GTmetrix REST API v2.0 in R在 R 中使用 GTmetrix REST API v2.0
【发布时间】:2021-12-10 08:45:21
【问题描述】:

我正在尝试使用 GTmetrix 集成某些网站的性能测试。通过 API,我可以使用 Microsoft Excel 中的 SEO 连接器工具运行测试并提取结果。但是,它使用带有旧版本 API 的 xml,并且其中不提供一些新的测试。最新版本是2.0

xml 的链接在这里:GTmetrix XML for API 0.1

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

API 的文档可在API Documentation 获得。

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))

【问题讨论】:

    标签: r rest jsonlite gtmetrix


    【解决方案1】:

    2021 年 11 月 8 日更新:

    我创建了一个小型库以通过 R 与 GTmetrix 对话。其中包含一些基本的健全性检查,但显然这仍在进行中,并且存在(潜在的严重)错误。不过,请随意检查一下。希望得到一些反馈。

    # Install and load library.
    devtools::install_github("RomanAbashin/rgtmx")
    library(rgtmx)
    

    2021 年 11 月 12 日更新:现在可在 CRAN 上使用。 :-)

    # Install and load library.
    install_packages("rgtmx")
    library(rgtmx)
    

    开始测试(并获得结果)

    # 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 和其他元数据。

    # Minimal example #2.
    # Returns just the test ID and some meta data.
    result <- start_test("google.com", "[API_KEY]", wait_for_completion = FALSE)
    

    其他可选参数:location, browser, report, retention, httpauth_username, httpauth_password, adblock, cookies, video, stop_onload, throttle, allow_url, block_url, dns, simulate_device, user_agent, browser_width, browser_height, browser_dppx, browser_rotate.

    显示可用的浏览器

    show_available_browsers("[API_KEY]")
    

    显示可用位置

    show_available_locations("[API_KEY]")
    

    获取具体测试

    get_test("[TEST_ID]", "[API_KEY]")
    

    获取具体报告

    get_report("[REPORT_ID]", "[API_KEY]")
    

    获取所有测试

    get_all_tests("[API_KEY]")
    

    获取帐户状态

    get_account_status("[API_KEY]")
    

    原答案:

    其实很简单:

    0。设置测试参数。

    # 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。开始测试

    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

    test_id <- jsonlite::fromJSON(rawToChar(res_test_start$content))$data$id
    

    3。获取报告 ID

    # 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。获取报告

    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 库...

    【讨论】:

    • 这是完美的。非常感谢。我还有一个问题。有没有什么类似于我们在 Python 中等到测试完成的东西:test.fetch(wait_for_completion=True)
    • 除此之外,如果我们能够从gtmetrix.com/api/2.0/locations获取可用位置列表会很好
    • 干杯@Mohanasundaram,我创建了一个小包来处理这些问题。看看吧。
    • 太棒了。我会在github上检查和更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多