6.5总结
今天上午做python大作业和工程数学实验
今天收获:
使用python调用百度地图api从地图上爬取数据,获得石家庄铁道大学内部部分建筑物地点的信息,并存入数据库
代码:
1 import requests 2 import json 3 4 # Step 1: 申请百度地图API密钥 5 api_key = 'your_api_key' # 请将your_api_key替换为你自己的API密钥 6 7 # Step 3: 发送API请求 8 def search_place(keyword, region): 9 url = 'http://api.map.baidu.com/place/v2/search' 10 params = { 11 'query': keyword, 12 'region': region, 13 'scope':'2', 14 'radius': 1000, # 搜索半径为1000米,可根据需要调整 15 'output': 'json', 16 'ak': api_key, 17 'page_size':'10', 18 'page_num':'1' 19 } 20 response = requests.get(url, params=params) 21 data = response.json() 22 23 # Step 4: 解析API返回数据 24 if data['status'] == 0: 25 for result in data['results']: 26 name = result['name'] 27 address = result['address'] 28 location = result['location'] 29 print(f'Name: {name}, Address: {address}, Location: {location}') 30 else: 31 print('No results found') 32 33 # 调用search_place函数进行地点检索 34 search_place('石家庄铁道大学', '石家庄铁道大学')