【问题标题】:Need help navigating lists when converting JSON into dataframe/CSV将 JSON 转换为数据框/CSV 时需要帮助导航列表
【发布时间】:2020-12-25 19:34:49
【问题描述】:

我正在尝试抓取一个 javascript 呈现的表格,在尝试了 selenium 和 phantomJS 之后,我决定 JSON 将是最简单的方法。但是,我对 R 很陌生,并且不太擅长处理列表,因此我无法将数据转换为我想要的表格格式。我查看了许多解决方案,但由于某种原因,它们并不能真正适用于我拥有的 JSON。

JSON 数据通过this URL 呈现。而this是表格所在的实际网站。

到目前为止,我所做的是尝试将 JSON 解析为 R 并将其强制转换为数据帧,基于我从 stackoverflow 上的大多数答案中看到的内容。

library(httr)
library(jsonlite)
rf <- GET(url) #the entire URL is very long so I'm not putting it here
rfc <- content(rf)

这样做会返回一个包含四个元素的大列表,rfc。然后我应用以下函数。

library(httr)
library(jsonlite)     
json_file <- lapply(rfc, function(x) {
      x[sapply(x, is.null)] <- NA
      unlist(x)
    })

这会返回一个错误:

Error in x[sapply(x, is.null)] <- NA : invalid subscript type 'list'

鉴于我只需要列表的第二个元素,即信息所在的位置,因此我尝试对其进行子集化:

  json_file <- lapply(rfc[2], function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

这会返回一个大列表,大小为 12mb。当我尝试使用 as.data.frame 将其强制转换为数据帧时,R 返回我对 1 个变量的 506472 个观察值。不同的列都被压缩成一个,标题也不见了。

谁能告诉我应该怎么做?有一个免费的在线 JSON 到 CSV converter here 可以完美地满足我的需求。这是它产生的结果:

很遗憾,这不是解决方案。因为我打算在 Shiny 中运行它,所以我想在 R 中做所有事情。感谢任何帮助,谢谢。

【问题讨论】:

    标签: r json dataframe csv web-scraping


    【解决方案1】:

    您需要获取元素rfc$data$DailyProductionAndFlowList,它本身实际上是一个单行数据框的列表,并将它们绑定在一起。您需要先覆盖 NULL 值:

    df <- do.call(rbind, lapply(rfc$data$DailyProductionAndFlowList, function(x) {
      x[sapply(x, is.null)] <- "NULL"
      as.data.frame(x, stringsAsFactors = FALSE)
    }))
    

    为了向您展示结果是合理的,我将其放在tibble 中以便更好地打印:

    as_tibble(df)
    #> # A tibble: 3,997 x 11
    #>    GasDate FacilityId FacilityName LocationId LocationName Demand Supply
    #>    <chr>        <int> <chr>             <int> <chr>         <dbl>  <dbl>
    #>  1 2020-0~     520047 Eastern Gas~     520008 Sydney        94.4     0  
    #>  2 2020-0~     520047 Eastern Gas~     520009 Canberra      16.5     0  
    #>  3 2020-0~     520047 Eastern Gas~     530015 Longford Hub   0     234. 
    #>  4 2020-0~     520047 Eastern Gas~     590011 Regional - ~  22.4     0  
    #>  5 2020-0~     520047 Eastern Gas~     590012 Regional - ~   2.68   19.4
    #>  6 2020-0~     520047 Eastern Gas~     520008 Sydney       113.      0  
    #>  7 2020-0~     520047 Eastern Gas~     520009 Canberra      19.7     0  
    #>  8 2020-0~     520047 Eastern Gas~     530015 Longford Hub   0     225. 
    #>  9 2020-0~     520047 Eastern Gas~     590011 Regional - ~  27.5     0  
    #> 10 2020-0~     520047 Eastern Gas~     590012 Regional - ~   5.05   20.1
    #> # ... with 3,987 more rows, and 4 more variables: TransferIn <dbl>,
    #> #   TransferOut <dbl>, HeldInStorage <chr>, LastUpdated <chr>
    
    

    【讨论】:

      【解决方案2】:

      另一种方法:

      library( data.table )
      library( rjson )
      
      #location of  data
      json.url = "https://aemo.com.au/aemo/api/v1/GasBBReporting/DailyProductionAndFlow?FacilityIds=540093,580010,540101,544261,540047,530030,540083,540096,580020,540071,540077,520075,540059,520054,520090,540094,540098,540080,540090,540086,540050,540097,540055,520047,540089,540070,540092,530071,530042,540088,540075,544253,540061,530038,530039,530040,580100,580040,540064,530043,550050,550045,550046,550054,520053,530061,520060,580050,540084,530041,530044,580060,580070,540065,550052,530060,540058,540085,540102,540073,540057,540095,544260,540110,540040,540082,540072,540062,540103,550061,550060,540060,540066,540067,540076,540068,580210,570050,540051,532005,530110,540045,540046,540091,580030,540069,540087,580180,540074&FromGasDate=07/08/2020&ToGasDate=07/09/2020"
      #retrieve lastest data
      mydata <- data.table::rbindlist( rjson::fromJSON( file = json.url )$data$DailyProductionAndFlowList, 
                                       use.names = TRUE, fill = TRUE )
      

      【讨论】:

        猜你喜欢
        • 2021-10-18
        • 2020-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-19
        相关资源
        最近更新 更多