【问题标题】:How to find the API associated with a web application?如何找到与 Web 应用程序关联的 API?
【发布时间】:2016-11-18 20:05:55
【问题描述】:

我想从这个Center for Disease Control site 获取 1996 年至 2016 年应通报的军团病病例的数据。我已设法使用 RSocrata 包并仅使用 Socrata 和Open Data Network 检索 2014 年至 2016 年的数据。

我将如何检索 2014 年之前的其余信息?

这是我在 2014、2015 和 2016 年使用的代码:

  #Legionellosis data 

  df.leg2014 <- read.socrata("https://data.cdc.gov/resource/cmap-p7au.json")#2014
  df.leg2015 <- read.socrata("https://data.cdc.gov/resource/haxn-dihy.json") #2015
  df.leg2016 <- read.socrata("https://data.cdc.gov/resource/wg57-d6dj.json") #2016

非常感谢任何建议!

【问题讨论】:

  • 看起来他们发布到 data.cdc.gov 的所有内容都是 2014-2016 (data.cdc.gov/…)。对于较旧的数据,您必须通过 wonder.cdc.gov。
  • @chrismetcal 是的,这就是 Wonder 应用程序所在的位置,其中包含我的所有数据。我只是不知道如何在不逐个导出文件的情况下获取我的数据。任何建议。

标签: r socrata


【解决方案1】:

您必须添加自己的列标题,并使用此功能循环您的年和周。有一些年/周组合,return an empty table 或数据为otherwise unavailable。它似乎也返回不同数量的列。有些有 12 个,有些有 10 个。不知道发生了什么。该函数有一行可以删除全部为 NA 的列。您可能想注释掉该行。我试图在代码中解释这一点。我没有很好地测试它。

read_MMWR_table <- function(year=1997, week=18){
  url <- paste0("https://wonder.cdc.gov/mmwr/mmwr_reps.asp?mmwr_year=",
                year, 
                "&mmwr_week=",
                week, 
                "&mmwr_table=2B&request=Export&mmwr_location=")
  tmp <- readLines(url)
  if(grepl("DOCTYPE html", tmp[1])){
    ret <- data.frame()
    print("No table returned, no data...")
  }else{
    start <- which(tmp=="tab delimited data:") + 1
    if(grepl("DOCTYPE html", tmp[1])){
      ret <- data.frame()
      print("No records found for this week / year")
    }else{
      end <- min(which(tmp=="")[which(tmp=="") > 20])
      df <- read.table(textConnection(tmp[start:end]), sep="\t", skip=19, 
                       stringsAsFactors=FALSE, header=FALSE)
      # remove all NA cols
      df <- df[,colSums(is.na(df))<nrow(df)]
      ret <- df
    }
  }
  return(ret)
}

df <- read_MMWR_table(year=2001, week=12)
> head(df)
            V1    V2    V3 V4  V5 V6 V7 V8 V9 V10
1 W.N. CENTRAL 2,534 3,688 61 102 11  5  2 11  14
2        Minn.   411   723  -   -  1  1  -  8   6
3         Iowa   202   224  -   -  2  2  -  -   -
4          Mo. 1,013 1,803 58  98  5  2  1  3   3
5      N. Dak.     9    11  -   -  -  -  -  -   -
6      S. Dak.    47    61  -   -  -  -  -  -   -

【讨论】:

  • 此解决方案似乎类似于我在此处发布的同事尝试的 (stackoverflow.com/questions/40616596/…)。但是,就像你一样,我担心网络抓取,因为军团病的位置在表中发生了变化。标题发生了变化,所以我想看看如何避免由于标题的变化而导致网页抓取。
  • 这里没有简单的答案。数据就在那里,我提供了一个函数,可以让你完成 75% 的工作。读取标题相当简单,它们列在每个文件的顶部。你将不得不投入一些工作......
猜你喜欢
  • 2018-11-01
  • 2016-12-10
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
相关资源
最近更新 更多