由于calleridtest关闭了,想找一个比较靠谱的查询方案,发现 Twilio的API还不错,它直接走去查 LIDB权威库,精准度很高。费用每次0.01$,新用户注册有15$试用,1500次应该是足够了。
1、前置准备:
- https://console.twilio.com/ 注册 Twilio 账号,在后台获取
ACCOUNT_SID和AUTH_TOKEN,并填写到下方代码里。 - 安装依赖库:
pip install twilio - 将下方的 Python 代码保存为
twilio_caller_id.py。
2、使用方法:在终端运行脚本,直接加电话号码(建议使用 E.164 格式,如 +1…):
python twilio_caller_id.py +1234567890
3、查询代码:
#!/usr/bin/env python3
"""Twilio Lookup API - Caller ID 查询脚本"""
import sys
from twilio.rest import Client
# Twilio 凭证
ACCOUNT_SID = "填写你的SID"
AUTH_TOKEN = "填写你的TOKEN"
def lookup_caller_id(phone_number: str) -> dict:
"""查询电话号码的 Caller ID 信息"""
client = Client(ACCOUNT_SID, AUTH_TOKEN)
# 使用 Lookup v2 API 查询 caller_name
result = client.lookups.v2.phone_numbers(phone_number).fetch(
fields="caller_name"
)
return {
"phone_number": result.phone_number,
"country_code": result.country_code,
"caller_name": result.caller_name,
"valid": result.valid,
}
def normalize_phone(phone: str) -> str:
"""格式化号码,自动添加 +1 前缀"""
phone = phone.strip().replace("-", "").replace(" ", "").replace("(", "").replace(")", "")
if not phone.startswith("+"):
phone = "+1" + phone
return phone
def main():
if len(sys.argv) < 2:
print("用法: python twilio_caller_id.py <电话号码>")
print("示例: python twilio_caller_id.py 4155551234")
sys.exit(1)
phone = normalize_phone(sys.argv[1])
print(f"查询号码: {phone}\n")
try:
info = lookup_caller_id(phone)
print(f"格式化号码: {info['phone_number']}")
print(f"国家代码: {info['country_code']}")
print(f"号码有效: {info['valid']}")
if info["caller_name"]:
print(f"来电显示: {info['caller_name'].get('caller_name', 'N/A')}")
print(f"类型: {info['caller_name'].get('caller_type', 'N/A')}")
else:
print("来电显示: 无数据")
except Exception as e:
print(f"查询失败: {e}")
sys.exit(1)
if __name__ == "__main__":
main()