如何通过 Python 请求传递 curl 选项



我正在使用 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)

希望这会起作用!

最新更新