Как скачать JSON файлов данных с использованием API из Бюро переписей - PullRequest
0 голосов
/ 04 мая 2020

Я пытаюсь использовать R для загрузки данных по международной торговле из Бюро переписей. Они предоставляют API, который возвращает данные JSON, как в следующем примере. Я не знаком с API и JSON. Как я могу запросить API через R?

{
      "@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/

1 Ответ

1 голос
/ 04 мая 2020

Самый простой способ - отправить запрос GET в API с помощью httr::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)

Вы можете посмотреть здесь , как работать с API в R.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...