基于Dijkstra算法的郑州地铁路径规划
2021-03-03 07:28
标签:需要 index header rto os x get process 经纬度 excel 需要引入geopy库 pip install geopy 安装即可 效果展示: 基于Dijkstra算法的郑州地铁路径规划 标签:需要 index header rto os x get process 经纬度 excel 原文地址:https://www.cnblogs.com/memory-ming/p/14398070.htmlimport requests
from bs4 import BeautifulSoup
import pandas as pd
import json
import os
from tqdm import tqdm
from collections import defaultdict
import pickle
import itertools
from geopy.distance import geodesic
#爬取郑州地铁数据
def spyder():
#获得郑州的地铁信息
print(‘正在爬取郑州地铁信息...‘)
url=‘http://zz.bendibao.com/ditie/linemap.shtml‘
user_agent=‘Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11‘
headers = {‘User-Agent‘: user_agent}
r = requests.get(url, headers=headers)
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, ‘lxml‘)
all_info = soup.find_all(‘div‘, class_=‘line-list‘)
df=pd.DataFrame(columns=[‘name‘,‘site‘])
for info in tqdm(all_info):
title=info.find_all(‘div‘,class_=‘wrap‘)[0].get_text().split()[0].replace(‘线路图‘,‘‘)
station_all=info.find_all(‘a‘,class_=‘link‘)
for station in station_all:
station_name=station.get_text()
longitude,latitude=get_location(station_name,‘郑州‘)
temp={‘name‘:station_name,‘site‘:title,‘longitude‘:longitude,‘latitude‘:latitude}
df =df.append(temp,ignore_index=True)
df.to_excel(‘./subway.xlsx‘,index=False)
def get_location(keyword,city):
#获得经纬度
user_agent=‘Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50‘
headers = {‘User-Agent‘: user_agent}
url=‘http://restapi.amap.com/v3/place/text?key=‘+keynum+‘&keywords=‘+keyword+‘&types=&city=‘+city+‘&children=1&offset=1&page=1&extensions=all‘
data = requests.get(url, headers=headers)
data.encoding=‘utf-8‘
data=json.loads(data.text)
result=data[‘pois‘][0][‘location‘].split(‘,‘)
return result[0],result[1]
def compute_distance(longitude1,latitude1,longitude2,latitude2):
#计算2点之间的距离
user_agent=‘Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50‘
headers = {‘User-Agent‘: user_agent}
url=‘http://restapi.amap.com/v3/distance?key=‘+keynum+‘&origins=‘+str(longitude1)+‘,‘+str(latitude1)+‘&destination=‘+str(longitude2)+‘,‘+str(latitude2)+‘&type=1‘
data=requests.get(url,headers=headers)
data.encoding=‘utf-8‘
data=json.loads(data.text)
result=data[‘results‘][0][‘distance‘]
return result
def get_graph():
print(‘正在创建pickle文件...‘)
data=pd.read_excel(‘./subway.xlsx‘)
#创建点之间的距离
graph=defaultdict(dict)
for i in range(data.shape[0]):
site1=data.iloc[i][‘site‘]
if i
文章标题:基于Dijkstra算法的郑州地铁路径规划
文章链接:http://soscw.com/index.php/essay/59415.html