利用百度地图API测量深圳产业办公及公寓的通勤时间
2020-05-08 04:19
1781 查看
利用百度地图API测量深圳产业办公及公寓的通勤时间,占坑待补充
import asyncio import aiohttp import time import xlrd import xlwt import json #打开产办和公寓的地址,并将数据存入字典 xl=xlrd.open_workbook(r'D:\xx\深圳产业办公及公寓数据.xlsx',formatting_info=False) sheet1=xl.sheet_by_index(0) nrows=sheet1.nrows chanyes={} boyus={} tasks=[] result_finall=[] for i in range(1,nrows): a=sheet1.cell(i,2).value b=sheet1.cell(i,1).value chanyes[sheet1.cell(i,0).value]=(str(sheet1.cell(i,2).value)+","+str(sheet1.cell(i,1).value)) sheet2=xl.sheet_by_index(2) nrows2=sheet2.nrows for i in range(50,nrows2): boyus[sheet2.cell(i,0).value]=(str(sheet2.cell(i,3).value)+","+str(sheet2.cell(i,2).value)) print(len(boyus),boyus) print(len(chanyes),chanyes) print('我完成提取了',time.time()) y=0 #构造获取距离时间函数并返回r.text() async def getjuli(qidian,zhongdian,boyu,chanye): url = "http://api.map.baidu.com/directionlite/v1/transit" ak = "GMDxyc6PASRARvsgw8BzbAnfTH6Euah9" #finurl = url+"origin="+qidian+"&destination="+zhongdian+"&ak="+ak canshu={'origin':qidian,'destination':zhongdian,'ak':ak} async with sem: global y async with aiohttp.ClientSession() as session: async with session.get(url=url,params=canshu) as r: a= await r.text(encoding='windows-1251') try: c= json.loads(a) dura = round(c['result']['routes'][0]['duration'] / 60) print('起点:{},终点:{},两点间通行时间为{}分钟'.format(chanye,boyu,dura)) result_finall.append([boyu,chanye,dura]) return result_finall except Exception as e: print('起点:{},终点:{},错误是{},'.format(boyu,chanye,e)) result_finall.append([boyu, chanye, e]) finally: y = y + 1 print(f'第{y}次爬取') #构造主函数及协程池 def async_main(): loop = asyncio.get_event_loop() for boyu,boyudizhi in boyus.items(): for chanye, chanyedizhi in chanyes.items(): [tasks.append(asyncio.ensure_future(getjuli(boyudizhi,chanyedizhi,boyu,chanye)))] loop.run_until_complete(asyncio.wait(tasks)) loop.close() if __name__ == '__main__': start = time.time() print(start) sem = asyncio.Semaphore(500) async_main() print(f'cost time: {time.time() - start}s') wb = xlwt.Workbook() sheetnew = wb.add_sheet('产业办公及公寓的距离', cell_overwrite_ok=True) i=0 for result in result_finall: sheetnew.write(i, 0, result[0]) sheetnew.write(i, 1, result[1]) sheetnew.write(i, 2, result[2]) print('{}存储成功{}'.format(result,i)) i=i+1 wb.save(r'D:\xxx.xls') print(f'一切完成cost time: {time.time() - start}s')cyincy 原创文章 2获赞 0访问量 156 关注 私信
相关文章推荐
- 时间都去哪儿了——利用User Timing API测量应用性能
- 利用Node.js了解与测量HTTP所花费的时间详解
- 利用arduino实现车速的测量,判断两次信号的时间间隔
- 利用Navigation Timing测量页面加载时间
- 利用产品思维,我将通勤时间缩短41.7%
- 利用JavaScript实现在浏览器状态栏显示停留时间
- 【Java】SpringMVC项目利用Shiro设置固定时间内密码登录重试次数限制
- TRIZ发明问题解决理论——本质是分析问题中的矛盾,利用资源(时间空间物质能量功能信息等)来解决矛盾从而解决问题——抽象出来:问题是什么,为什么?
- php利用时间生成随机函数
- 利用ajax不需刷新页面来获取当前时间
- 怎样利用细碎时间达到整体学习的效果
- 利用百度地图API实现gps坐标到百度坐标的批量坐标转换(如1000个点)
- 利用javascript得到系统当前时间(含有3种格式)
- 百度地图高级实例1-如何利用【百度地图API】,制作房产酒店地图?(上)
- C++ 时间获取和时间测量
- 测量程序运行时间
- 利用Timer类实现"显示时间"的功能
- 引导时间利用
- 利用组策略锁定系统时间
- 3.利用SimpleDateFormat()把字符串格式的时间转化为Date类型的时间