[英]How to get data from an api in r?
我是 api 的新手,并且在api
中遇到了一小段代码来检索我想在r
中复制的数据python
python 代码:
import requests
import json
from datetime import date
import time
import smtplib, ssl
#API URL
url = 'http://cdn-api.co-vin.in/api/v2/admin/location/states'
headers = {'accept': 'application/json','Accept-Language' : 'hi_IN','User-Agent': 'Mozilla/4.0'}
result = requests.get(url, headers=headers)
#Print state ID
print(result.content.decode())
结果:
{"states":[{"state_id":1,"state_name":"安达曼和尼科巴群岛"},{"state_id":2,"state_name":"安得拉邦"},{"state_id":3," state_name":"阿鲁纳恰尔邦"},{"state_id":4,"state_name":"阿萨姆邦"},{"state_id":5,"state_name":"比哈尔邦"},{"state_id":6,"state_name ":"Chandigarh"},{"state_id":7,"state_name":"Chhattisgarh"},{"state_id":8,"state_name":"Dadra and Nagar Haveli"},{"state_id":37," state_name":"达曼和迪乌"},{"state_id":9,"state_name":"德里"},{"state_id":10,"state_name":"果阿"},{"state_id":11," state_name":"Gujarat"},{"state_id":12,"state_name":"Haryana"},{"state_id":13,"state_name":"喜马偕尔邦"},{"state_id":14,"state_name ":"查谟和孟加拉"}],"ttl":24}
关于 API 的信息:
url :'http://cdn-api.co-vin.in/api/v2/admin/location/states'
来自: https://apisetu.gov.in/public/marketplace/api/cowin#/Metadata%20APIs/states
来自: https://github.com/cowinapi/developer.cowin/issues/339
(注意: cowin API仅限于在印度访问。所以我猜你们中的许多人将无法使用它。但如果您能建议一些代码更改,它仍然会有所帮助。)
我在谷歌上搜索了一下并尝试了下面的代码,但到目前为止它们都没有工作:
library(tidyverse)
library(rjson)
library(jsonlite)
library(RCurl)
library(httr)
states_url = 'http://cdn-api.co-vin.in/api/v2/admin/location/states'
headers = c({'accept' = 'application/json'},
{'Accept-Language' = 'hi_IN'},
{'User-Agent' = 'Mozilla/4.0'})
url(states_url, headers = headers)
GET(states_url)$content
GET(states_url)$headers
更新
我已经尝试过了,它没有给出错误,但不知道下一步该怎么做:
states_url = 'http://cdn-api.co-vin.in/api/v2/admin/location/states'
headers = c('accept' = {'application/json'},
'Accept-Language' = {'hi_IN'},
'User-Agent' = {'Mozilla/4.0'})
url(states_url, headers = headers)
与
描述“http://cdn-api.co-vin.in/api/v2/admin/location/states”class“url-wininet”
模式“r”
文字“文字”
打开“关闭”
可以读“是”
可以写“不”
GET(states_url, header = headers)$content
1 3c 21 44 4f 43 54 59 50 45 20 48 54 4d 4c 20 50 55 42 4c [20] 49 43 20 22 2d 2f 2f 57 33 43 2f 2f 44 54 44 20 48 504 4d [39] 3c 04 31 20 54 72 61 6e 73 69 74 69 6f 6e 61 6c [58] 2f 2f 45 4e 22 20 22 68 74 74 70 3a 2f 2f 77 77 77 2e 77 [77] 33 2e 6f 72 67 6f 45252 6d 6c 34 2f 6c 6f 6f 73 [96] 65 2e 64 74 64 22 3e 0a 3c 48 54 4d 4c 3e 3c 48 45 41 44 [115] 3e 3c 4d 45 54 41 20 48 54 54 50 459 2 3d 22 [134] 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22 20 43 4f 4e 54 45 [153] 4e 54 3d 22 74 65 78 74 2f 68 74 6d 6c 3b 20 63 68 61 7
str(GET(states_url))
List of 10
$ url : chr "http://cdn-api.co-vin.in/api/v2/admin/location/states"
$ status_code: int 403
$ headers :List of 9
..$ server : chr "CloudFront"
..$ date : chr "Tue, 25 May 2021 12:39:02 GMT"
..$ content-type : chr "text/html"
..$ content-length: chr "919"
..$ connection : chr "keep-alive"
..$ x-cache : chr "Error from cloudfront"
..$ via : chr "1.1 85ad220378d99bdabeb6c46016f1cf16.cloudfront.net (CloudFront)"
..$ x-amz-cf-pop : chr "BOM51-C1"
..$ x-amz-cf-id : chr "eeJq5ZtSJHZkLGoJZBTUL2xL5PcU2gjesnY7Qmg_kMnxZxZ1JUHPWA=="
..- attr(*, "class")= chr [1:2] "insensitive" "list"
$ all_headers:List of 1
..$ :List of 3
.. ..$ status : int 403
.. ..$ version: chr "HTTP/1.1"
.. ..$ headers:List of 9
.. .. ..$ server : chr "CloudFront"
.. .. ..$ date : chr "Tue, 25 May 2021 12:39:02 GMT"
.. .. ..$ content-type : chr "text/html"
.. .. ..$ content-length: chr "919"
.. .. ..$ connection : chr "keep-alive"
.. .. ..$ x-cache : chr "Error from cloudfront"
.. .. ..$ via : chr "1.1 85ad220378d99bdabeb6c46016f1cf16.cloudfront.net (CloudFront)"
.. .. ..$ x-amz-cf-pop : chr "BOM51-C1"
.. .. ..$ x-amz-cf-id : chr "eeJq5ZtSJHZkLGoJZBTUL2xL5PcU2gjesnY7Qmg_kMnxZxZ1JUHPWA=="
.. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
$ cookies :'data.frame': 0 obs. of 7 variables:
..$ domain : logi(0)
..$ flag : logi(0)
..$ path : logi(0)
..$ secure : logi(0)
..$ expiration: 'POSIXct' num(0)
..$ name : logi(0)
..$ value : logi(0)
$ content : raw [1:919] 3c 21 44 4f ...
$ date : POSIXct[1:1], format: "2021-05-25 12:39:02"
$ times : Named num [1:6] 0 0.242 0.283 0.284 0.321 ...
..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
$ request :List of 7
..$ method : chr "GET"
..$ url : chr "http://cdn-api.co-vin.in/api/v2/admin/location/states"
..$ headers : Named chr "application/json, text/xml, application/xml, */*"
.. ..- attr(*, "names")= chr "Accept"
..$ fields : NULL
..$ options :List of 2
.. ..$ useragent: chr "libcurl/7.64.1 r-curl/4.3.1 httr/1.4.2"
.. ..$ httpget : logi TRUE
..$ auth_token: NULL
..$ output : list()
.. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
..- attr(*, "class")= chr "request"
$ handle :Class 'curl_handle' <externalptr>
- attr(*, "class")= chr "response"
Show in New Window
http_status(GET(states_url))
$category
[1] "Client error"
$reason
[1] "Forbidden"
$message
[1] "Client error: (403) Forbidden"
stringi::stri_enc_detect(GET(states_url, header = headers)$content)
[[1]]
Encoding Language Confidence
1 ISO-8859-1 en 0.54
2 ISO-8859-2 ro 0.26
3 UTF-8 0.15
4 UTF-16BE 0.10
5 UTF-16LE 0.10
6 Shift_JIS ja 0.10
7 GB18030 zh 0.10
8 EUC-JP ja 0.10
9 EUC-KR ko 0.10
10 Big5 zh 0.10
11 ISO-8859-9 tr 0.06
12 IBM424_rtl he 0.02
13 IBM424_ltr he 0.01
content(GET(states_url, header = headers), encoding = "UTF-8")
{html_document}
<html>
[1] <head>\n<meta http-equiv="Content-Type" content="text/htm ...
[2] <body>\n<h1>403 ERROR</h1>\n<h2>The request could not be ...
content(GET(states_url, header = headers), encoding = "ISO-8859-1")
{html_document}
<html>
[1] <head>\n<meta http-equiv="Content-Type" content="text/htm ...
[2] <body>\n<h1>403 ERROR</h1>\n<h2>The request could not be ...
python 代码图像:
您制作headers
但从不将它们包含在对GET
的调用中。 在那里使用它们。
GET(states_url, add_headers(headers))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.