有谁知道如何使用 python 或 R 中的 API 获取 WASDE(世界农业供需估计)报告基本数据?
Does anyone know how to fetch WASDE (World Agricultural supply and demand estimates) report fundamental data by using API in python or R?
https://usda.library.cornell.edu/apidoc/index.html 网站提供了 API 文档。有人可以告诉我在 R 或 Python..
中获取数据所需的步骤吗
以下是在 R 中的实现方法。
首先,创建包含您注册时创建的电子邮件地址和密码的变量:
my_email <- "Niket_Chauhan@somewhere.com"
my_password <- "Passw0rd!"
您将需要在 json 字符串中将这些作为 POST 请求发送,以获得使用该服务的授权代码。我们现在将创建 json:
my_json <- paste0('{"auth": {"email":"', my_email,'","password":"', my_password, '"}}')
现在我们将使用 httr::POST
:
请求授权令牌
library(httr)
auth <- POST("https://usda.library.cornell.edu/user_token",
body = my_json, encode = "raw",
add_headers(`Content-Type` = "application/json"))
如果一切顺利,auth
现在包含我们的授权令牌。我们将在 'Authorization' header 中对服务器的每个后续请求使用此令牌:
token <- add_headers("Authorization" = paste("Bearer", content(auth, "parsed")$jwt))
所以现在我们可以使用 GET
和 POST
向 API 发送 http 请求。对于其中的大多数,我们可以只使用我们想要的 url,并确保我们包含了我们的 header.
res <- GET("https://usda.library.cornell.edu/api/v1/agency/findAll", token)
此处,res
是一个 http 响应 object,因此我们将其内容提取为文本以获取我们的 json 字符串:
cat(content(res, "text"))
#> [{"id":"waob_agency","title":["World Agricultural Outlook Board"],
#> "acronym":["WAOB"],"contact_email":["oce@oce.usda.gov"],"contact_phone":
#> ["202-720-5447"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.usda.gov/oce/commodity/"]},
#> {"id":"ers_agency","title":["Economic Research Service"],"acronym":
#> ["ERS"],"contact_email":["ers@ers.usda.gov"],"contact_phone":
#> ["202-694-5139"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.ers.usda.gov/"]},
#> {"id":"nass_agency","title":["National Agricultural Statistics Service"],
#> "acronym":["NASS"],"contact_email":["nass@nass.usda.gov"],
#> "contact_phone":["800-727-9540"],"location_city":["Washington"],
#> "location_state":["DC"],"homepage_url":["http://www.nass.usda.gov/"]},
#> {"id":"fas_agency","title":["Foreign Agricultural Service"],"acronym":
#> ["FAS"],"contact_email":["fas@fas.usda.gov"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.fas.usda.gov/"]},{"id":"ams_agency","title":
#> ["Agricultural Marketing Service"],"acronym":["AMS"],"contact_email":
#> ["ams@ams.usda.gov"],"contact_phone":["202-720-8998"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.ams.usda.gov/"]}]
https://usda.library.cornell.edu/apidoc/index.html 网站提供了 API 文档。有人可以告诉我在 R 或 Python..
中获取数据所需的步骤吗以下是在 R 中的实现方法。
首先,创建包含您注册时创建的电子邮件地址和密码的变量:
my_email <- "Niket_Chauhan@somewhere.com"
my_password <- "Passw0rd!"
您将需要在 json 字符串中将这些作为 POST 请求发送,以获得使用该服务的授权代码。我们现在将创建 json:
my_json <- paste0('{"auth": {"email":"', my_email,'","password":"', my_password, '"}}')
现在我们将使用 httr::POST
:
library(httr)
auth <- POST("https://usda.library.cornell.edu/user_token",
body = my_json, encode = "raw",
add_headers(`Content-Type` = "application/json"))
如果一切顺利,auth
现在包含我们的授权令牌。我们将在 'Authorization' header 中对服务器的每个后续请求使用此令牌:
token <- add_headers("Authorization" = paste("Bearer", content(auth, "parsed")$jwt))
所以现在我们可以使用 GET
和 POST
向 API 发送 http 请求。对于其中的大多数,我们可以只使用我们想要的 url,并确保我们包含了我们的 header.
res <- GET("https://usda.library.cornell.edu/api/v1/agency/findAll", token)
此处,res
是一个 http 响应 object,因此我们将其内容提取为文本以获取我们的 json 字符串:
cat(content(res, "text"))
#> [{"id":"waob_agency","title":["World Agricultural Outlook Board"],
#> "acronym":["WAOB"],"contact_email":["oce@oce.usda.gov"],"contact_phone":
#> ["202-720-5447"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.usda.gov/oce/commodity/"]},
#> {"id":"ers_agency","title":["Economic Research Service"],"acronym":
#> ["ERS"],"contact_email":["ers@ers.usda.gov"],"contact_phone":
#> ["202-694-5139"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.ers.usda.gov/"]},
#> {"id":"nass_agency","title":["National Agricultural Statistics Service"],
#> "acronym":["NASS"],"contact_email":["nass@nass.usda.gov"],
#> "contact_phone":["800-727-9540"],"location_city":["Washington"],
#> "location_state":["DC"],"homepage_url":["http://www.nass.usda.gov/"]},
#> {"id":"fas_agency","title":["Foreign Agricultural Service"],"acronym":
#> ["FAS"],"contact_email":["fas@fas.usda.gov"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.fas.usda.gov/"]},{"id":"ams_agency","title":
#> ["Agricultural Marketing Service"],"acronym":["AMS"],"contact_email":
#> ["ams@ams.usda.gov"],"contact_phone":["202-720-8998"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.ams.usda.gov/"]}]