Idealista API 如何筛“无家具”出租房:布尔参数只能传 true 怎么办

发布于 2026年10月30日

想找没有家具的出租房,很多人的第一反应是传 furnished=false;想排除禁止养宠物的房子,就传 petsAllowed=false。这个做法默认布尔参数有“真”和“假”两个可用取值。可是 list-home-properties 的说明并不是这样写的。本文把那句说明当作一个需要验证的论断,而不是可以直接相信的事实。

为什么 furnished=false 不能当“无家具”用

这个接口一共有 49 个布尔标志,覆盖房屋类型、卧室、浴室、楼宇配套、出售条件和出租条款。参考文档写明:标志只会以 true 发送,不传就表示不筛选。文档给出的“假”的表达方式,就是干脆不写这个参数。

furnished 并不在这 49 个标志里,它是一个字符串选择项:留空表示不限,furnished 表示带家具,furnishedKitchen 表示只有厨房带家具,而且只能配合 operation=rent 使用。petsAllowed 才是真正的布尔标志,同样只适用于出租。

于是整套请求语言里,每个参数都有“不限”的写法,每个标志都有“必须有”的写法,唯独没有“必须没有”的写法。无家具、禁止宠物都无法直接请求。

用最小的一页做三次探测:不传、true、false

实验只需要三次请求,彼此只差一个参数:petsAllowed 不传、传 true、传 false。其余参数完全相同,页面也尽量小,因为只需要读返回的汇总字段。目前记录里被接受的每页数量只有 maxItems=5,所以探测就用它。参考文档说 maxItems=0 只返回数量,但这个模式没有验证过,不要盲目换上去。

true 和 false 要按文本发送。Python 的 urlencode 会把布尔值 True 转成首字母大写的 True,这和文档里的写法并不一致。

curl 'https://api.opendata-api.com/api/project/idealista/list-home-properties?key=YOUR_API_KEY&locationId=0-EU-ES-28-07-001-079&location=es&locale=es&operation=rent&order=relevance&maxItems=5&numPage=1'

只有在你确实要发请求时,才把 YOUR_API_KEY 换成自己获准使用的平台密钥,并且不要让它出现在日志或共享内容里。打开本文或复制代码不会发出任何请求,真正运行命令才会。

这个接口目前只记录过一次无筛选的出售搜索,证据里没有任何 true 或 false 的请求,所以本文不断言 false 会怎样。可能的结果有三种:与不传一致、与 true 一致,或者得到自己独立的结果甚至被拒绝。文末的脚本会在运行时判断属于哪一种,而后面的做法在三种结果下都成立。

先看 total 和回显的 filter,再看房源行

读取顺序是:先看 HTTP 状态,再看网关的 code,最后才看返回内容。只有 HTTP 200 并不等于业务成功。

已记录的马德里市无筛选出售搜索返回了 total 19806,在 maxItems=5 下 totalPages 为 3962,totalAppliedFilters 为 0,filter 为 {"locationName": "Madrid"}。探测只需要这几个字段,不需要 elementList 里的房源行。

至于启用某个标志后 filter 和 totalAppliedFilters 会怎么变,记录里没有,而这恰恰就是探测要测的东西:真正生效的标志应当让 total 和已应用筛选数发生变化,被忽略的标志则会让它们与不传时保持一致。

“无家具”是一道减法,不是一个参数

既然没有参数能表达否定,就用计数来得到它:出租房总数,减去 furnished=furnished 的出租房数,再减去 furnished=furnishedKitchen 的出租房数。三次计数要连续发出,其余参数完全一致,并记下时间,因为市场数据在两次请求之间会变化。每次请求只带一个选择值,所以两个带家具的集合是各自独立的结果;文档没有说明一条广告会不会同时出现在两边,这也是之后要对照行列表的原因之一。

宠物条件可以用同样的办法:出租房总数减去 petsAllowed=true 的出租房数,剩下的是没有标注允许宠物的出租房。

没勾选“带家具”不等于确认无家具

相减得到的是“没有标注带家具”的广告数量,其中也包括根本没写家具情况的广告。所以请把这个数字命名成 not_marked_furnished 之类,在产品里不要把这些房源直接标成“无家具”。同样,没有标注允许宠物也不等于禁止宠物。

已探测的房源卡片带有 features 对象,但本文不断言其中是否写明家具情况。在基于行级字段写逻辑之前,请先查看一条出租房源卡片。

多个布尔参数叠加:是收窄还是放宽要实测

参考文档没有说明多个标志如何组合,两种房屋类型可能是“或”,也可能是“且”。与其猜测,不如实测:对标志 A 和 B,分别请求 total(A)、total(B) 和 total(A 与 B)。组合后的总数不高于较小的单项总数,说明行为像收窄;不低于较大的单项总数,则像放宽。落在两者之间的情况,要先看过具体房源行,再决定能不能基于它做减法。

翻页账单:全量页数加带家具页数

只数数量只需要几次请求,而列出房源行则要翻页,因为补集无法直接请求。你必须翻完全部出租房源的所有页,再翻完带家具房源的所有页,然后剔除带家具的编号。账单就是 pages(all) + pages(furnished),如果把“只有厨房带家具”也算作带家具,还要加上 pages(furnishedKitchen)。

页数等于 ceil(total / maxItems)。文档所述的最大每页数量是 40,超出的数值会被忽略。已记录的马德里出售总数 19806,每页 5 条是 3962 页,每页 40 条则是 496 页。这些是出售数据,仅用来演示算术,出租总数并没有记录。每一页都是一次独立的平台请求,所以请先确定数量,再决定要不要翻页。

对两边使用相同的价格区间、sinceDate 时间窗口或更小的 locationId,既能缩小账单,也能让相减结果保持一致。

一个先计数、再翻页的完整脚本

脚本会先做 petsAllowed 探测,再统计出租房和带家具房的数量,打印翻页账单;只有加上 --fetch 并且账单不超过 PAGE_BUDGET 时,才会真正列出房源行。locationId 请先用 get-suggestions 查出。

import json
import math
import os
import sys
import urllib.error
import urllib.parse
import urllib.request

API_KEY = os.environ.get("PLATFORM_API_KEY", "YOUR_API_KEY")
BASE = "https://api.opendata-api.com/api/project/idealista/list-home-properties"
SEARCH = {
    "locationId": "0-EU-ES-28-07-001-079",  # Madrid municipality
    "location": "es",
    "locale": "es",
    "operation": "rent",
    "order": "relevance",
}
PROBE_SIZE = 5     # the only page size recorded as accepted
PAGE_SIZE = 40     # documented maximum
PAGE_BUDGET = 300  # refuse to page when the bill is larger


def call(extra, size, page=1):
    query = {"key": API_KEY, **SEARCH, "maxItems": size, "numPage": page, **extra}
    url = BASE + "?" + urllib.parse.urlencode(query)
    try:
        with urllib.request.urlopen(url, timeout=60) as response:
            status, body = response.status, json.load(response)
    except urllib.error.HTTPError as error:
        return {"error": f"HTTP {error.code}"}
    if status != 200 or body.get("code") != 200:
        return {"error": f"HTTP {status}, gateway code {body.get('code')!r}"}
    payload = body.get("data")
    if not isinstance(payload, dict) or not isinstance(payload.get("total"), int):
        return {"error": "no total in payload"}
    return payload


def readout(payload):
    if "error" in payload:
        return payload
    keys = ("total", "totalPages", "totalAppliedFilters", "filter")
    return {key: payload.get(key) for key in keys}


def probe(flag):
    omitted = readout(call({}, PROBE_SIZE))
    true = readout(call({flag: "true"}, PROBE_SIZE))
    false = readout(call({flag: "false"}, PROBE_SIZE))
    if "error" in omitted:
        raise SystemExit(omitted["error"])
    if "error" in false:
        verdict = "false was rejected"
    elif false == omitted:
        verdict = "false behaves like omitted"
    elif false == true:
        verdict = "false behaves like true"
    else:
        verdict = "false is its own result; inspect rows before using it"
    return omitted, true, false, verdict


def total_for(extra):
    payload = call(extra, PROBE_SIZE)
    if "error" in payload:
        raise SystemExit(payload["error"])
    return payload["total"]


def collect(extra):
    cards, number, last = {}, 1, 1
    while number <= last:
        page = call(extra, PAGE_SIZE, number)
        if "error" in page:
            raise SystemExit(page["error"])
        last = page.get("totalPages") or 1
        for card in page.get("elementList", []):
            cards[card["propertyCode"]] = card
        number += 1
    return cards


def main(fetch=False):
    print("petsAllowed probe:", *probe("petsAllowed"), sep="\n  ")
    everything = total_for({})
    furnished = total_for({"furnished": "furnished"})
    kitchen = total_for({"furnished": "furnishedKitchen"})
    remainder = everything - furnished - kitchen
    bill = sum(math.ceil(n / PAGE_SIZE) for n in (everything, furnished, kitchen))
    print(f"all={everything} furnished={furnished} kitchen={kitchen} "
          f"not_marked_furnished={remainder} page_bill={bill}")
    if not fetch or bill > PAGE_BUDGET:
        return
    rows = collect({})
    for extra in ({"furnished": "furnished"}, {"furnished": "furnishedKitchen"}):
        for code in collect(extra):
            rows.pop(code, None)
    print(f"listed {len(rows)} rows against {remainder} counted")
    for code, card in rows.items():
        print(code, card.get("price"), card.get("url"))


if __name__ == "__main__":
    main(fetch="--fetch" in sys.argv)

第一次运行时先不要加 --fetch,把打印出的探测结果连同日期一起保存下来。如果最后列出的行数与计数得到的余数相差很大,说明两次请求之间市场发生了变化,或者两个带家具集合有重叠,请换一个更窄的搜索范围重新运行。