通过 Python 使用 CURL 调用 API

roh*_*hit 1 python curl

我正在使用一个API来识别车牌;我得到这个curl命令:

如何在PYTHON中使用curl实现这样的调用?

curl "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1?url=https%3A%2F%2Fwww.havenondemand.com%2Fsample-content%2Fvideos%2Fgb-plates.mp4&source_location=GB&apikey=695e513c-xxxx-xxxx-xxxx-xxxxxxxxxx"

curl -X POST --form "url=https://www.havenondemand.com/sample-content/videos/gb-plates.mp4" --form "source_location=GB" --form "apikey=695e513c-xxxx-xxxx-a666-xxxxxxxxxx" https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1
Run Code Online (Sandbox Code Playgroud)

mas*_*nun 5

在 Python 中,使用requests模块是一个更好的选择。首先安装它:

pip install requests
Run Code Online (Sandbox Code Playgroud)

然后执行以下操作:

import requests

API_URL = "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1"

data = {
    "url": "https://www.havenondemand.com/sample-content/videos/gb-plates.mp4",
    "source_location": "GB",
    "apikey": "695e513c-xxxx-xxxx-a666-xxxxxxxxxx"
 }

response = requests.post(API_URL, data)
print(response.json())
Run Code Online (Sandbox Code Playgroud)

data基本上,任何表单字段都应该作为键值对放入字典中。我们这里使用requests.post()函数。该函数将目标 URL 作为第一个参数。并将表单值作为第二个参数。

我们得到一个响应对象。您可以通过打印 的值来查看原始响应response.content。但是,如果您知道响应是 JSON,则可以使用该json()方法解析响应并返回 Python 数据类型(字典)。