【问题标题】:How to Download JSON Data Files Using API from Census Bureau如何使用人口普查局的 API 下载 JSON 数据文件
【发布时间】:2020-05-04 15:32:38
【问题描述】:

我正在尝试使用 R 从人口普查局下载国际贸易数据。它们提供了一个返回 JSON 数据的 API,如下例所示。我不熟悉 API 和 JSON。如何通过 R 查询 API?

{
      "@context": "https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld",
      "@id": "https://api.census.gov/data/timeseries/eits/ftd.json",
      "@type": "dcat:Catalog",
      "conformsTo": "https://project-open-data.cio.gov/v1.1/schema",
      "describedBy": "https://project-open-data.cio.gov/v1.1/schema/catalog.json",
      "dataset": [
            {
                  "c_dataset": [
                        "timeseries",
                        "eits",
                        "ftd"
                        ],
                  "c_geographyLink": "https://api.census.gov/data/timeseries/eits/ftd/geography.json",
                  "c_variablesLink": "https://api.census.gov/data/timeseries/eits/ftd/variables.json",
                  "c_examplesLink": "https://api.census.gov/data/timeseries/eits/ftd/examples.json",
                  "c_groupsLink": "https://api.census.gov/data/timeseries/eits/ftd/groups.json",
                  "c_valuesLink": "https://api.census.gov/data/timeseries/eits/ftd/values.json",
                  "c_documentationLink": "http://www.census.gov/developer/",
                  "c_isTimeseries": true,
                  "c_isCube": true,
                  "c_isAvailable": true,
                  "@type": "dcat:Dataset",
                  "title": "Time Series Economic Indicators Time Series -: U.S. International Trade in Goods and Services",
                  "accessLevel": "public",
                  "bureauCode": [
                        "006:07"
                        ],
                  "description": "The U.S. Census Bureau.s economic indicator surveys provide monthly and quarterly data that are timely, reliable, and offer comprehensive measures of the U.S. economy. These surveys produce a variety of statistics covering construction, housing, international trade, retail trade, wholesale trade, services and manufacturing. The survey data provide measures of economic activity that allow analysis of economic performance and inform business investment and policy decisions. Other data included, which are not considered principal economic indicators, are the Quarterly Summary of State & Local Taxes, Quarterly Survey of Public Pensions, and the Manufactured Homes Survey.  For information on the reliability and use of the data, including important notes on estimation and sampling variance, seasonal adjustment, measures of sampling variability, and other information pertinent to the economic indicators, visit the individual programs' webpages - http://www.census.gov/cgi-bin/briefroom/BriefRm.",
                  "distribution": [
                        {
                              "@type": "dcat:Distribution",
                              "accessURL": "https://api.census.gov/data/timeseries/eits/ftd",
                              "description": "API endpoint",
                              "format": "API",
                              "mediaType": "application/json",
                              "title": "API endpoint"
                        }
                        ],
                  "contactPoint": {
                        "fn": "Economic Indicators Mail List",
                        "hasEmail": "econ.indicators@census.gov"
                  },
                  "identifier": "http://api.census.gov/data/id/EITSFTD",
                  "keyword": [
                        ],
                  "license": "http://creativecommons.org/publicdomain/zero/1.0/Public Domain",
                  "modified": "2017-02-23",
                  "programCode": [
                        "006:007"
                        ],
                  "references": [
                        "http://www.census.gov/developers/"
                        ],
                  "spatial": "United States",
                  "temporal": "January 1992 - Current",
                  "publisher": {
                        "@type": "org:Organization",
                        "name": "U.S. Census Bureau",
                        "subOrganizationOf": {
                              "@type": "org:Organization",
                              "name": "U.S. Department Of Commerce",
                              "subOrganizationOf": {
                                    "@type": "org:Organization",
                                    "name": "U.S. Government"
                              }
                        }
                  }
            }
            ]
}

更多详情:API 可以在这里找到:https://www.census.gov/data/developers/data-sets/international-trade.html

数据可以从这里手动下载,需要登录:https://usatrade.census.gov/

【问题讨论】:

    标签: r json api


    【解决方案1】:

    最简单的方法是使用httr::GET() 向API 发送GET 请求。下面的 URL 取自他们的示例。您可以修改 URL 参数(例如将 year=2013 更改为 year=2000)以检索不同的结果。最后,我使用data.table::rbindlist() 将嵌套列表绑定到data.table/data.frame 对象中。

    require(httr)
    require(data.table)
    
    url = "https://api.census.gov/data/timeseries/intltrade/exports/hs?get=DISTRICT,DIST_NAME,E_COMMODITY,E_COMMODITY_LDESC,ALL_VAL_MO,ALL_VAL_YR,VES_VAL_MO,VES_VAL_YR&YEAR=2013&MONTH=12&DISTRICT=13"
    
    res = GET(url)
    cont = content(res) # parses the API result (recognizes JSON)
    
    dat = rbindlist(cont)
    

    您可以查看here,了解如何在 R 中使用 API。

    【讨论】:

    • 非常感谢!这篇文章很有帮助。仍然不明白其中的一些,但我正在了解 API 以及它现在如何工作得更好一些。真的很感激!
    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 2015-04-13
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多