使用Python通过CURL调用API [英] CALL with CURL an API through Python

查看:418
本文介绍了使用Python通过CURL调用API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​进行车牌识别;我得到这个curl命令:

I am working with an API for license plate recognition ; and i get this curl command :

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

How to implement such call with curl in PYTHON?

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


推荐答案

在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).

这篇关于使用Python通过CURL调用API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆