首页 > 解决方案 > 将 Datafiniti API 连接到 R(将 Python 代码翻译成 R)

问题描述

我希望在 R 中连接 Datafiniti API。由于他们发布的一些信息,我已经能够在 Python 中连接到它,但是作为初学者,我无法将此代码翻译成 R。通过对 SO 的研究到目前为止,我相信我需要使用GET但不确定如何合并request_headersrequest_data分割 Python 代码。下面我输入了 Python 中的代码。关于如何更改此 Python 代码以使其适合 R 的任何建议?

import requests
import urllib.parse
import json

# Set your API parameters here.
API_token = token
format = 'JSON'
Query = query
num_records = 2
download = False

request_headers = {
    'Authorization': 'Bearer ' + API_token,
    'Content-Type': 'application/json',
}
request_data = {
    'query': Query,
    'format': format,
    'num_records': num_records,
    'download': download
}

# Make the API call.
r = requests.post('https://api.datafiniti.co/v4/properties/search',json=request_data,headers=request_headers);

# Do something with the response.
if r.status_code == 200:
    print(r.content)
else:
    print('Request failed')

到目前为止,这是我在 R 中尝试过的(没有奏效):

API_token <-  token
format <- 'JSON'
Query <- query
num_records <- 2
download <- FALSE

request_headers = paste(
  "'Authorization':", 'Bearer ', API_token,
  "'Content-Type':", "'application/json'"
)


request_data <- list('query'= Query, 'format'= format, 'num_records'=num_records, 'download'=download)

path <- "https://api.datafiniti.co/v4/properties/search"

GET(
  url = path,
  query= query,
  content_type_json(),
  add_headers(request_headers)
)

标签: pythonrapi

解决方案


推荐阅读