如何通过API获取Rightmove邮编对应ID以生成爬虫访问URL
Rightmove批量邮编转对应ID实现方案
Rightmove没有公开的官方API供第三方查询邮编映射ID,但可以直接复用其官网前端的位置自动补全接口完成批量查询,不需要手动逐个查找。
核心逻辑
Rightmove搜索栏的输入联想功能会实时返回和输入关键词匹配的位置信息,返回结果里直接包含构造URL需要的locationIdentifier字段,只要传入邮编作为关键词,筛选出精确匹配的邮编条目,就能提取到对应ID。
具体实现步骤
- 配置常规浏览器请求头,必须携带合法的
User-Agent字段,否则会被反爬机制拦截。 - 向站点的位置联想接口发起GET请求,将待查询的标准化邮编(去多余空格、统一转大写)作为搜索关键词传入。
- 解析接口返回的JSON数据,筛选类型为
POSTCODE、显示名称和查询邮编完全一致的条目,从locationIdentifier字段中提取^符号后的数字,就是目标ID。 - 批量查询时添加1-3秒的随机请求间隔,避免请求频率过高触发IP封禁。
参考代码
import requests import time import random import pandas as pd # 替换为自己浏览器的实际UA即可 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36", "Referer": "https://www.rightmove.co.uk/" } def get_postcode_id(postcode: str): postcode = postcode.strip().upper() params = {"searchTerm": postcode, "locationType": "REGION"} try: resp = requests.get( "https://www.rightmove.co.uk/house-prices/suggest.html", params=params, headers=headers, timeout=10 ) resp.raise_for_status() for item in resp.json(): if item.get("type") == "POSTCODE" and item.get("displayName", "").strip().upper() == postcode: return item["locationIdentifier"].split("^")[1] return None except Exception as e: print(f"查询邮编{postcode}失败:{str(e)}") return None if __name__ == "__main__": # 替换为需要批量查询的邮编列表 postcode_list = ["SY3 9EB"] result = [] for pc in postcode_list: pc_id = get_postcode_id(pc) result.append({"Postcode": pc, "ID": pc_id}) time.sleep(random.uniform(1, 3)) # 导出为两列结构的映射表 pd.DataFrame(result).to_csv("postcode_id_map.csv", index=False)
注意事项
- 你示例URL中出现的
%5E是^的URL编码结果,不是ID的组成部分,对应邮编SY3 9EB的真实ID是纯数字4203018,拼接URL时使用POSTCODE^{id}的格式做URL编码即可,不需要额外加5E前缀。 - 如果单次查询量级超过千条,建议搭配代理IP池使用,降低被站点封禁的风险。
- 如果接口返回空结果,先检查邮编格式是否正确,部分无房产数据的偏远邮编可能不存在对应ID。
内容的提问来源于stack exchange,提问作者Mensa 23
相关产品推荐
相关产品推荐

