import requests
import time
def search_place(api_key, keyword, region):
search_url = "http://api.map.baidu.com/place/v2/search"
detail_url = "http://api.map.baidu.com/place/v2/detail"
page_num = 0 # 起始页码
while True:
search_params = {
"query": keyword,
"region": region,
"output": "json",
"ak": api_key,
"page_num": page_num, # 当前页码
"page_size": 10 # 每页的结果数(最多10)
}
response = requests.get(search_url, params=search_params)
if response.status_code == 200:
data = response.json()
if data.get("status") == 0 and data.get("results"):
for place in data.get("results", []):
name = place.get("name")
address = place.get("address")
location = place.get("location")
lat = location.get("lat") if location else None
lng = location.get("lng") if location else None
uid = place.get("uid")
detail_params = {
"uid": uid,
"output": "json",
"scope": 2, # 设置为2获取详细信息
"ak": api_key,
}
detail_response = requests.get(detail_url, params=detail_params)
if detail_response.status_code == 200:
detail_data = detail_response.json()
if detail_data.get("status") == 0:
detail_info = detail_data.get("result", {})
telephone = detail_info.get("telephone", "无联系方式")
print(f"名称: {name}")
print(f"地址: {address}")
print(f"经度: {lng}")
print(f"纬度: {lat}")
print(f"联系方式: {telephone}")
print("-" * 40)
else:
print(f"无法获取 {name} 的详细信息,错误: {detail_data.get('message')}")
else:
print(f"请求详细信息失败,状态码: {detail_response.status_code}")
page_num += 1
else:
# 如果没有更多结果,结束循环
break
else:
print("请求失败,状态码:", response.status_code)
break
api_key = "" # 替换为您的百度API密钥
keyword = "酒店" # 搜索关键词
region = "武汉市江汉区" # 搜索区域