且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python通过CURL调用API

更新时间:2022-04-14 00:12:32

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

In Python, using the requests module is a much better option. Install it first:

pip install requests

然后执行以下操作:

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())

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

Basically, any form fields should go inside the data dictionary as key value pairs. We use requests.post() function here. The function takes the target URL as the first parameter. And the form values as the second parameter.

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

We get a response object back. You can see the raw response by printing out the value of response.content. However, if you know that the response is JSON, you can use the json() method to parse the response and get back Python data type (dictionary).