【问题标题】:How to get real estate data with Idealista API?如何使用 Idealista API 获取房地产数据?
【发布时间】:2017-02-22 18:00:56
【问题描述】:

我一直在尝试使用 Idealista (https://www.idealista.com/) 网站的 API 来检索房地产数据的信息。

由于我不熟悉 OAuth2,所以目前无法获得令牌。我刚刚获得了 api 密钥、秘密和一些关于如何挂载 http 请求的基本信息。

我会很感激这个 API 的功能示例(最好是 Python),或者一些关于处理 OAuth2 和 Python 的更通用的信息。

【问题讨论】:

    标签: python oauth2


    【解决方案1】:

    经过几天的研究,我想出了一个基本的 Python 代码,可以从 Idealista API 中检索房地产数据。

    def get_oauth_token():
    http_obj = Http()
    url = "https://api.idealista.com/oauth/token"
    apikey= urllib.parse.quote_plus('Provided_API_key')
    secret= urllib.parse.quote_plus('Provided_API_secret')
    auth = base64.encode(apikey + ':' + secret)
    body = {'grant_type':'client_credentials'}
    headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8','Authorization' : 'Basic ' + auth}
    resp, content = http_obj.request(url,method='POST',headers=headers, body=urllib.parse.urlencode(body))
    return content
    

    此函数将返回带有 OAuth2 令牌和会话时间(以秒为单位)的 JSON。之后,查询 API,就这么简单:

    def search_api(token):
    http_obj = Http()
    url = "http://api.idealista.com/3.5/es/search?center=40.42938099999995,-3.7097526269835726&country=es&maxItems=50&numPage=1&distance=452&propertyType=bedrooms&operation=rent"
    headers = {'Authorization' : 'Bearer ' + token}
    resp, content = http_obj.request(url,method='POST',headers=headers)
    return content
    

    这一次,我们将在 content var 中找到我们正在寻找的数据,同样是 JSON。

    【讨论】:

    • cool,http_obj 是什么实例?需要导入哪些库?谢谢!
    • 嘿@Nabla,已经有一段时间了,但我想我正在使用httplib2。但是,您可以对其他框架执行相同的操作,例如 python 3 中的请求。
    • 嗨,好点。你在哪里找到了这方面的文档?
    • 当时我在请求访问后收到了一些从 api@idealista.com 发送的文档。
    【解决方案2】:

    这是我的代码,正在改进#3...运行正常!为了我!!!! 只输入你的 apikey 和你的密码(秘密)...

    import pandas as pd
    import json
    import urllib
    import requests as rq
    import base64
    
    def get_oauth_token():
        url = "https://api.idealista.com/oauth/token"    
        apikey= 'your_api_key' #sent by idealista
        secret= 'your_password'  #sent by idealista
        auth = base64.b64encode(apikey + ':' + secret)
        headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' ,'Authorization' : 'Basic ' + auth}
        params = urllib.urlencode({'grant_type':'client_credentials'})
        content = rq.post(url,headers = headers, params=params)
        bearer_token = json.loads(content.text)['access_token']
        return bearer_token
    
    def search_api(token, url):  
        headers = {'Content-Type': 'Content-Type: multipart/form-data;', 'Authorization' : 'Bearer ' + token}
        content = rq.post(url, headers = headers)
        result = json.loads(content.text)['access_token']
        return result
    
    country = 'es' #values: es, it, pt
    locale = 'es' #values: es, it, pt, en, ca
    language = 'es' #
    max_items = '50'
    operation = 'sale' 
    property_type = 'homes'
    order = 'priceDown' 
    center = '40.4167,-3.70325' 
    distance = '60000'
    sort = 'desc'
    bankOffer = 'false'
    
    df_tot = pd.DataFrame()
    limit = 10
    
    for i in range(1,limit):
        url = ('https://api.idealista.com/3.5/'+country+'/search?operation='+operation+#"&locale="+locale+
               '&maxItems='+max_items+
               '&order='+order+
               '&center='+center+
               '&distance='+distance+
               '&propertyType='+property_type+
               '&sort='+sort+ 
               '&numPage=%s'+
               '&language='+language) %(i)  
        a = search_api(get_oauth_token(), url)
        df = pd.DataFrame.from_dict(a['elementList'])
        df_tot = pd.concat([df_tot,df])
    
    df_tot = df_tot.reset_index()
    

    【讨论】:

    • 有很多不必要的代码,注释掉的代码。清理它并只发布必要的内容
    • 你好@Ivan。您可以使用此 API 过滤单个真实状态代理的结果吗?
    【解决方案3】:

    这不能被标记为正确答案,因为

    auth = base64.encode(apikey + ':' + secret)
    body = {'grant_type':'client_credentials'}
    headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8','Authorization' : 'Basic ' + auth}
    

    会给你TypeError:

    can only concatenate str (not "bytes") to str 
    

    由于base64encode返回一个字节类型的对象...

    确实 Idealista API 在文档方面非常有限,但我认为这是一种更好的方法,因为我不使用不必要的库(仅限本机):

    #first request
    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)
    

    这仅适用于 python 请求和 base64 模块...

    问候

    【讨论】:

      【解决方案4】:

      我发现了一些错误。至少,我无法运行它。 我相信,我对此有所改进:

      import pandas as pd
      import json
      import urllib
      import requests as rq
      import base64
      
      def get_oauth_token(): 
      
          url = "https://api.idealista.com/oauth/token"    
      
          apikey= 'your_api_key' #sent by idealist
          secret= 'your_password' #sent by idealista
          apikey_secret = apikey + ':' + secret
          auth = str(base64.b64encode(bytes(apikey_secret, 'utf-8')))[2:][:-1]
      
          headers = {'Authorization' : 'Basic ' + auth,'Content-Type': 'application/x-www-form- 
          urlencoded;charset=UTF-8'}
          params = urllib.parse.urlencode({'grant_type':'client_credentials'}) #,'scope':'read'
          content = rq.post(url,headers = headers, params=params)
          bearer_token = json.loads(content.text)['access_token']
      
          return bearer_token
      
      
      def search_api(token, URL):  
          headers = {'Content-Type': 'Content-Type: multipart/form-data;', 'Authorization' : 'Bearer ' + token}
          content = rq.post(url, headers = headers)
          result = json.loads(content.text)
      
          return result
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-29
        • 2016-05-27
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多