python - 将 Datafiniti API 连接到 R(将 Python 代码翻译成 R)
问题描述
我希望在 R 中连接 Datafiniti API。由于他们发布的一些信息,我已经能够在 Python 中连接到它,但是作为初学者,我无法将此代码翻译成 R。通过对 SO 的研究到目前为止,我相信我需要使用GET
但不确定如何合并request_headers
和request_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)
)
解决方案
推荐阅读
- javascript - 给定一个值为数组的对象,返回对 [key, array.length] 的最有效方法是什么?
- php - PHP 邮件在邮件中显示密件抄送地址
- arduino-uno - Arduino mills() 溢出
- java - 字符串如何相互比较?
- linux - sudoers:不能运行 2 个命令列表,其中第二个具有 glob * 并拒绝第一个
- node.js - 您如何在预中间件中实现 mongo 聚合管道?
- c# - Google 文字转语音延迟
- android - 从内部侦听器中删除 Firestore 快照侦听器
- excel - 如何在不定义行号的情况下生成 SUM(A+B)?
- android - Android MVVM:从广播接收器观察数据库变化