python - 如何使用Idealista API获取特性数据?

标签 python oauth-2.0

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

由于我不熟悉 OAuth2,所以到目前为止我无法获得 token 。我刚刚获得了 api key 、 secret 和一些关于如何挂载 http 请求的基本信息。

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

最佳答案

经过几天的研究,我想出了一个基本的 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 token 和以秒为单位的 session 时间的 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

这次我们会在内容变量中找到我们正在寻找的数据,同样是一个 JSON。

关于python - 如何使用Idealista API获取特性数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40023931/

相关文章:

python - wxPython - py2exe - exe 文件旧窗口

curl - 使用多个uri获取redirect_uri_mismatch错误

Facebook 登录和 OpenID Connect

api - Reddit API Oauth2 "user required"

python - 如何让随机大小的形状从屏幕顶部落到底部?

python - 如何在 Python 中使用 Pandas 从特定列中查找重复行元素的最大绝对值并显示行和列索引

python - 为使用 Opencv 2.3 的算法创建 Python 包装器

Python Tools for Visual Studio - 告诉 IDE 参数的类型

google-apps-script - 使用 OAuth 与 StackExchange API 和 Apps 脚本进行授权时遇到问题

oauth-2.0 - OAuth 2.0 用于 Google 登录 : Getting Unknown Host Exception