【问题标题】:How to call curl command for API from python script如何从 python 脚本调用 API 的 curl 命令
【发布时间】:2019-10-24 03:50:34
【问题描述】:

我想调用 curl 函数 python 脚本来调用 API。这是怎么做到的。

我从终端执行了它的工作正常。

curl --header "Authorization: Api-Key ABCD1234"  https://api.lat.com/western/v1/forecasts?locodes=PLWRE,DEHLI


{"results":[{"place":{"type":"locode","value":"PLWRO"},"measures":[{"ts":1571896800000,"t2m":10.72,"t_min":10.53,"t_max":11.99,"wspd":8,"dir":"SE","wgust":12,"rh2m":87,"prsmsl":1012,"skcover":"clear","precip":0.0,"snowd":0,"thunderstorm":"N","fog":"H"}]},{"place":{"type":"locode","value":"DEHAM"},"measures":[{"ts":1571896800000,"t2m":10.79,"t_min":10.3,"t_max":10.9,"wspd":13,"dir":"ESE","wgust":31,"rh2m":97,"prsmsl":1008,"skcover":"partly_cloudy","precip":0.0,"snowd":0,"thunderstorm":"N","fog":"H"}]}]}sh-4.2$ 

如何做到这一点?

【问题讨论】:

    标签: python-3.x pandas curl https


    【解决方案1】:

    您可以使用 requests 库进行 cURL 调用。

    import requests
    
    url = "https://api.lat.com/western/v1/forecasts?locodes=PLWRE,DEHLI"
    headers = {
        "Authorization": "Api-Key ABCD1234"
        }
    
    response = requests.get(url=url, headers=headers)
    

    您可以使用response.json() 获得 JSON 格式的响应。

    【讨论】:

      【解决方案2】:

      我推荐这个资源: https://curl.trillworks.com/

      它只是利用 requests 模块将你的 curl 语句转换为参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多