如何在Periolita中与python一起使用API

2024-05-20 03:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以在python中使用理想化Api,还是一种更简单的方法? 我以前没试过这个。 我有钥匙和秘密,还有一份地籍代码清单,所以想知道每个代码的租金


Tags: 方法代码api秘密钥匙租金理想化地籍
1条回答
网友
1楼 · 发布于 2024-05-20 03:42:54

嗨,这对我使用Python 3.8很有用

首先,您需要从Idealista获得代币:

import json
import requests
import base64

def get_token():
    API_KEY= "YOUR_API_KEY"
    SECRET= "YOUR_SECRET"
    message = API_KEY + ":" + SECRET
    auth = "Basic " + base64.b64encode(message.encode("ascii")).decode("ascii")

    headers_dic = {"Authorization" : auth,
                   "Content-Type" : "application/x-www-form-urlencoded;charset=UTF-8"}

    params_dic = {"grant_type" : "client_credentials",
                  "scope" : "read"}

    r = requests.post("https://api.idealista.com/oauth/token",
                      headers = headers_dic,
                      params = params_dic)

    bearer_token = json.loads(r.text)['access_token']

    return bearer_token

使用该令牌,您现在可以访问搜索api:

def get_search():
    headers_dic = {"Authorization" : "Bearer " + TOKEN,
                   "Content-Type" : "application/x-www-form-urlencoded"}

    params_dic = {"operation" : "rent",
                  "locationId" : "0-EU-ES-01",
                  "propertyType" : "homes"}

    r = requests.post("https://api.idealista.com/3.5/es/search",
                      headers = headers_dic,
                      params = params_dic)

    result_json = json.loads(r.text)

    return result_json

我发现对于西班牙,locationId从0-EU-ES-010-EU-ES-56。我没有尝试其他国家

相关问题 更多 >