Files
F6--/张阳脚本/百度地图测试.ipynb
T
2026-01-30 11:28:35 +08:00

187 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-05-12T03:37:52.859828Z",
"start_time": "2025-05-12T03:37:51.819641Z"
}
},
"source": [
"import pandas as pd\n",
"import requests\n",
"\n",
"# 读取Excel文件\n",
"df = pd.read_excel(r\"C:\\Users\\Administrator.DESKTOP-7IC2USJ\\Desktop\\地址.xlsx\",sheet_name=\"Sheet2\")\n",
"print(df)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 省 市 区/县 地址 门店店名\n",
"0 上海市 市辖区 杨浦区 国和路 上海车杰汽车服务\n",
"1 上海市 市辖区 杨浦区 周家嘴路 SunRise Garage汽美工作室\n",
"2 上海市 市辖区 闵行区 泰虹路 上海 11区洗美工作室\n",
"3 上海市 市辖区 闵行区 中春路 上海龙腾汽车用品批发\n",
"4 上海市 市辖区 闵行区 华友路 膜幻视佳量子膜上海中心店\n",
"5 天津市 市辖区 河西区 解放南路环渤海汽车城 天津久航汽车装具\n",
"6 天津市 市辖区 河西区 解放路环渤海汽车城D区 科奇装具\n",
"7 天津市 市辖区 宝坻区 钰华街道 博士汽车音响改装\n"
]
}
],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-12T03:51:38.882104Z",
"start_time": "2025-05-12T03:51:38.837039Z"
}
},
"cell_type": "code",
"source": [
"# 百度地图API的URL和密钥\n",
"url = \"https://api.map.baidu.com/place/v3/search\"\n",
"ak = \"jKHRQ8B8LcccdK6qGVWYbvjhMNIhak31\" # 替换为你的百度地图API密钥\n",
"sn = \"SK iX68jJkgCs5rXkuH9l02I9jwFrqu5IB9\"\n",
"\n",
"# 遍历Excel表中的每一行\n",
"for index, row in df.iterrows():\n",
" # 构造查询参数\n",
" params = {\n",
" \"query\": row['门店店名'], # 使用门店店名作为查询关键词\n",
" \"region\": row['省'], # 指定查询的城市\n",
" \"output\": \"json\",\n",
" \"ak\": ak,\n",
" \"sn\": sn,\n",
" \"scope\": 2 # 获取详细信息\n",
" }\n",
"\n",
" # 发送请求\n",
" response = requests.get(url, params=params)\n",
" result = response.json()\n",
" print(result)\n",
" break\n",
"# pois = data.get(\"pois\",[])\n",
"# if len(pois)==0:\n",
"# print(f\"未找到门店店名:{shop_name}\")\n",
"# continue\n",
"# for poi in pois:\n",
"# poi.update({\"shop_name\":row['门店店名']})\n",
"# all_data.append(poi)\n",
"#\n",
"# df = pd.DataFrame(all_data)\n",
"# df.to_csv(\"pois1.csv\",index=False)\n",
"\n",
"\n"
],
"id": "a78d2f1b2810ada0",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'status': 211, 'message': 'APP SN校验失败'}\n"
]
}
],
"execution_count": 8
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
" # 检查是否有结果\n",
" if result['status'] == 0 and 'results' in result and result['results']:\n",
" # 获取第一个结果的电话号码\n",
" telephone = result['results'][0].get('telephone', '')\n",
" # 将电话号码写入新的列\n",
" df.at[index, '电话号码'] = telephone\n",
" else:\n",
" # 如果没有找到结果,写入空字符串\n",
" df.at[index, '电话号码'] = ''\n",
"\n",
"# 保存修改后的Excel文件\n",
"df.to_excel('updated_file.xlsx', index=False) # 替换为保存后的文件路径"
],
"id": "670eba13d2a1d820"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-12T03:54:24.013939Z",
"start_time": "2025-05-12T03:54:23.896204Z"
}
},
"cell_type": "code",
"source": [
"# -*- coding: utf-8 -*-\n",
"import requests\n",
"import time\n",
"import json\n",
"import hashlib\n",
"from urllib import parse\n",
"\n",
"def GetBaiduResponse(addtress):\n",
" # 以get请求为例http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=你的ak\n",
" queryStr = '/geocoding/v3/?address=%s&output=json&ak=jKHRQ8B8LcccdK6qGVWYbvjhMNIhak31' % addtress\n",
" # 对queryStr进行转码,safe内的保留字符不转换\n",
" encodedStr = parse.quote(queryStr, safe=\"/:=&?#+!$,;'@()*[]\")\n",
" # 在最后直接追加上yoursk\n",
" rawStr = encodedStr + 'iX68jJkgCs5rXkuH9l02I9jwFrqu5IB9'\n",
" #计算sn\n",
" sn = (hashlib.md5(parse.quote_plus(rawStr).encode(\"utf8\")).hexdigest())\n",
" #由于URL里面含有中文,所以需要用parse.quote进行处理,然后返回最终可调用的url\n",
" url = parse.quote(\"http://api.map.baidu.com\"+queryStr+\"&sn=\"+sn, safe=\"/:=&?#+!$,;'@()*[]\")\n",
" res = requests.get(url, headers={\"content-type\": \"application/json\"})\n",
"\n",
" return res.content.decode()\n",
"\n",
"if __name__ == '__main__':\n",
" res = GetBaiduResponse('上海')\n",
" print(res)\n"
],
"id": "8f6b2269366d39ca",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"status\":0,\"result\":{\"location\":{\"lng\":121.48053886017651,\"lat\":31.235929042252015},\"precise\":0,\"confidence\":20,\"comprehension\":100,\"level\":\"城市\"}}\n"
]
}
],
"execution_count": 11
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}