【问题标题】:Scraping data from JavaScript graph to R将数据从 JavaScript 图形抓取到 R
【发布时间】:2020-05-06 17:09:14
【问题描述】:

我正在尝试自动从article 中的图 1:相对于 2019 年的用电量获取数据。我从普通页面抓取没有问题,但是这个图表是在 JS 中完成的,我不知道如何继续或在哪里找到图表使用的数据。

【问题讨论】:

    标签: javascript r web-scraping


    【解决方案1】:

    这是一个相对棘手的抓取工作。您要查找的数据位于 Eric Truett 链接的页面中。它采用 JSON 字符串的格式,隐藏在 Javascript 调用的文本中。因此,您需要的步骤是:

    1. 识别实际包含数据的页面(@EricTruett 已经完成)
    2. 以文本字符串形式获取 html 页面
    3. 去掉你想要的字符串部分
    4. 解析 JSON
    5. 获取结果列表中包含所需数据的元素
    6. 将该元素转换为您想要的格式

    我可以向您展示如何执行第 2 步、第 3 步和第 4 步,但第 5 步和第 6 步取决于您究竟需要输出什么,而您没有在问题中指定。我刚刚在这里猜到了:

    # Step 1: Get the correct url (usually done via developer tools in a browser)
    uri <- "https://e.infogram.com/8dc2a0f6-6c05-4e0a-91c3-4122c56989d9?src=embed"
    
    # Step 2: Read the html into memory as a single text string:
    page <- paste(readLines(uri), collapse = "\n")
    
    # Step 3: Strip out the JSON you need. This can only really be done by scanning the
    #         html for the data you want and finding unique delimiters at either end,
    #         carving out the string with regexes and tidying up either end if needed.
    page <- strsplit(page, "\"data\":\\[{3}", useBytes = TRUE)[[1]][2]
    json <- paste0("[[[", strsplit(page, "]]]", useBytes = TRUE)[[1]][1], "]]]")
    
    # Step 4: Parse the JSON. Use an existing library such as jsonlite for this
    map_data <- jsonlite::fromJSON(json)
    
    # Step 5: Find the element(s) you want in the resulting data structure. Here, the
    #         result is a list with several elements, and from visual inspection, element
    #         9 appears to be a nice tabular array containing useful data
    useful_array <- map_data[[9]]
    
    # Step 6: Arrange the result however you like. Here, I have just selected out some
    #         useful columns and converted to a tibble for pretty printing:
    df <- dplyr::as_tibble(map_data[[9]][,c(1, 2, 6)])
    df <- setNames(df[which(df$V2 != ""), ], c("Country", "Percent", "Change"))
    

    结果如下所示:

    df
    #> # A tibble: 28 x 3
    #>    Country  Percent Change 
    #>    <chr>    <chr>   <chr>  
    #>  1 Austria  90.44%  -10.00%
    #>  2 Belgium  85.39%  -15.00%
    #>  3 Bulgaria 94.54%  -5.00% 
    #>  4 Croatia  87.16%  -13.00%
    #>  5 Denmark  98.82%  -1.00% 
    #>  6 Estonia  97.20%  -3.00% 
    #>  7 Finland  92.40%  -8.00% 
    #>  8 France   85.87%  -14.00%
    #>  9 Germany  91.86%  -8.00% 
    #> 10 Greece   108.48% 8.00%  
    #> # ... with 18 more rows
    

    您可能需要在map_data 中挖掘才能获得您需要的实际数据。

    【讨论】:

      【解决方案2】:

      主页嵌入来自https://e.infogram.com/8dc2a0f6-6c05-4e0a-91c3-4122c56989d9?src=embed 的图形。如果你查看嵌入页面的来源,数据在一个javascript变量window.infographicData中。

      【讨论】:

      • 感谢您的帮助,但我最感兴趣的是如何将数据获取到 R data.frame。不幸的是,我不知道任何 JS。
      猜你喜欢
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 2021-02-16
      • 2015-06-25
      • 1970-01-01
      • 2021-04-20
      相关资源
      最近更新 更多