2024-06-01 01:51:05 发布
网友
我一直在尝试使用理想化网站(https://www.idealista.com/)的API来检索房地产数据的信息。在
因为我不熟悉OAuth2,所以到目前为止我还没能获得令牌。我刚刚得到了api密钥、秘密和一些如何挂载http请求的基本信息。在
我希望有一个例子(最好是Python)来说明这个API的功能,或者提供一些关于处理OAuth2和Python的更通用的信息。在
经过几天的研究,我想出了一个基本的python代码,可以从ide心itaapi中检索房地产数据。在
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,可以简单到:
这一次,我们将在content var中找到我们要查找的数据,同样是一个JSON。在
这是我的代码,改进3。。。这次跑得好!为了我!!!! 只需输入您的apikey和密码(secret)。。。在
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+ '¢er='+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()
经过几天的研究,我想出了一个基本的python代码,可以从ide心itaapi中检索房地产数据。在
此函数将返回一个带有OAuth2令牌的JSON,会话时间以秒为单位。之后,要查询API,可以简单到:
^{pr2}$这一次,我们将在content var中找到我们要查找的数据,同样是一个JSON。在
这是我的代码,改进3。。。这次跑得好!为了我!!!! 只需输入您的apikey和密码(secret)。。。在
相关问题 更多 >
编程相关推荐