我正在使用 Desk.com api。限制之一是它们只允许调用 500 页记录。如果您有超过500个页面需要下载,则需要使用curl -d选项对数据进行排序/过滤。通常,我通过将“since_id”选项设置为更高的 ID 并下载 500 个以上的页面来实现此目的。这本质上是告诉桌面数据库向我发送最多 500 页数据,since_id=x
通常我使用 os.popen() 在 python 中运行它,但我想尝试将其切换到 requests.get(),因为这应该在 Windows 设备上工作得更好。
os.popen("curl https://www.URL.com -u username:password -H 'Accept:application/json' -d 'since_id=someID&sort_field=id&sort_direction=asc' -G")
对于请求,我尝试了多种不同的方式来运行它,包括尝试像这样传递 -d 参数。
payload = '-d 'since_id=someID&sort_field=id&sort_direction=asc' -G'
payload(alternate) = "{"-d":"'since_id=someID&sort_field=id&sort_direction=asc'","-G":""}"
requests.get('https://www.URL.com',auth=('username','password'),data=payload)
老实说,在第二次尝试有效负载变量时,我不确定如何处理 -G。
I have tried the following.
*including '-G' in the "-d" value of the json as well as putting it in its own dict
*a few different variations including switching 'data' to 'params' on the requests.get line.
*Adding/removing single quotes on the -d value in the get request
curl中的
-d
参数对应于请求中的查询参数。像这样的东西应该有效:
payload = {'since_id': 'someID', 'sort_field': 'id', 'sort_direction': 'asc'}
requests.get('https://www.example.com', params=payload)
我假设 API 使用基本身份验证。
import requests
from requests.auth import HTTPBasicAuth
payload = {'something':'value'}
requests.get('http://myurl.com', auth=HTTPBasicAuth('user', 'pass'), params=Payload)
希望这会起作用!
我想发帖,这是
requests.post(url, headers=headers, json=data)
完整示例
curl -X POST localhost:8080 \
-H "Content-Type: application/cloudevents+json" \
-d '{
"specversion" : "1.0",
"type" : "example.com.cloud.event",
"source" : "https://example.com/cloudevents/pull",
"subject" : "123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"data" : "hello world"
}'
import requests
url = 'http://127.0.0.1:8080'
headers = {'Content-type': 'application/cloudevents+json'}
data = {
"specversion" : "1.0",
"type" : "example.com.cloud.event",
"source" : "https://example.com/cloudevents/pull",
"subject" : "123",
"id" : "A234-1234-1234",
"time" : "2018-04-05T17:31:00Z",
"data" : "hello world"
}
r = requests.post(url, headers=headers, json=data)