如何用Python通过Truecaller批量查询2K号码姓名?解决429错误与无结果问题
解决Truecaller批量查询号码姓名的问题
一、处理429请求限流错误
单次循环调用search_phonenumber会快速触发Truecaller的请求频率限制,导致429错误,可通过以下方式优化:
- 添加请求间隔+控制并发数
改用异步任务批量处理,同时设置请求间隔和并发数上限,避免短时间内发送过多请求:import asyncio from truecallerpy import search_phonenumber async def single_search(phone_number, country_code, installation_id): await asyncio.sleep(3) # 每次请求间隔3秒,可根据限流情况调整 return await search_phonenumber(phone_number, country_code, installation_id) async def batch_search(numbers, country_code, installation_id): # 限制同时请求的数量,这里设为5 semaphore = asyncio.Semaphore(5) async def bounded_search(phone): async with semaphore: return await single_search(str(phone), country_code, installation_id) tasks = [bounded_search(n) for n in numbers] return await asyncio.gather(*tasks) # 调用示例 your_numbers = ["1234567890", "9876543210"] # 替换为你的2000个号码 installation_id = "你的合法安装ID" results = asyncio.run(batch_search(your_numbers, "US", installation_id))
二、修复bulk_search无返回结果的问题
bulk_search失效通常是格式问题,按以下要求调整:
- 号码格式必须带国家码前缀
所有号码需要以+国家码开头,比如美国号码写成+1234567890,不能省略+号或国家码。 - 正确调用并解析结果
如果仍无结果,可能是目标号码未在Truecaller数据库中登记,或你的installation_id权限受限。import asyncio from truecallerpy import bulk_search # 正确的号码格式:逗号分隔带+号的号码 phone_numbers = "+11234567890,+19876543210" country_code = "US" installation_id = "你的合法安装ID" response = asyncio.run(bulk_search(phone_numbers, country_code, installation_id)) # 解析姓名 for entry in response: if "data" in entry and len(entry["data"]) > 0: name = entry["data"][0].get("name") print(f"号码: {entry['phoneNumber']}, 姓名: {name or '无记录'}")
三、进阶方案
- 轮换installation_id:准备多个合法获取的installation_id,轮流用于查询,降低单个ID的请求频率(注意遵守Truecaller服务条款)。
- 官方商业API:若批量查询需求长期存在,可申请Truecaller官方商业API,获得更高的请求限额,避免限流问题。
内容的提问来源于stack exchange,提问作者bishop1
相关产品推荐
相关产品推荐

