百度地图测距-终稿V3.0:

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
from selenium.webdriver import Chrome, ChromeOptions
import pandas as pd
import jsondef data_a(start, end):df = pd.DataFrame()distance = []# 清空数据try:driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/input').clear()driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/input').clear()driver.find_element_by_xpath("//div['routebox-input route-start']/input[@class='route-start-input']").send_keys(start)#起点driver.find_element_by_xpath("//div[@class='routebox-input route-end']/input[@class='route-end-input']").send_keys(end)#终点time.sleep(2)driver.find_element_by_id('search-button').click()  # 模拟点击time.sleep(3)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_0_0']").click()  # 点击起点time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击终点起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()#点击起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()  # 点击起点html = BeautifulSoup(driver.page_source, 'html.parser')source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")except:print("点击错误1")# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击终点起点try:time.sleep(4)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击终点起点time.sleep(3)print("执行")except:print("点击错误2")html = BeautifulSoup(driver.page_source, 'html.parser')# time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")# print("点击错误3")html = BeautifulSoup(driver.page_source, 'html.parser')# time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")with open('rr.html', 'w', encoding='utf-8') as f:  # 写入网页f.write(source)# time.sleep(1)# with open('rr.html', 'r', encoding='utf-8') as f:#打开网页#     html_a = BeautifulSoup(f, 'lxml')# 获取距离html.list = html.find_all('div', attrs={'class': 'navtrans-navlist-title'})for i, item in enumerate(html.list):try:distance.append(item.find_all('span')[2].text.strip('公里'))# print(item.find_all('span')[2].text.strip('公里'),)df["start"] = start,df["end"] = end,df["距离"] = (distance[0]),# df.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")# print(df)except:print(str(i), "网页获取错误")return dfdf = pd.DataFrame()
if __name__ == "__main__":opt = ChromeOptions()  # 创建Chrome参数对象opt.headless = False  # 把Chrome设置成可视化无界面模式,windows/Linux 皆可driver = Chrome(options=opt)# driver = webdriver.Chrome()url="https://map.baidu.com/?newmap=1&shareurl=1&l=12&tn=B_NORMAL_MAP&hb=B_SATELLITE_STREET&c=13376484,3517857"#"https://map.baidu.com/@13376484,3517857,12z"driver.get(url)#'https://map.baidu.com/'driver.find_element_by_xpath("//div[@class='searchbox-content-button right-button route-button loading-button']").click()  # 点击路线导航driver.find_element_by_xpath("//div[@class='searchbox-content-common route-tabs']/div[@class='tab-item drive-tab']/span").click()  # 点击驾车导航location = ['北京', '上海', '深圳', '合肥', '杭州']app = xw.App(visible=False, add_book=False)wb = app.books.open('distance.xlsx')# 创建一个worksheetsh = wb.sheets['distance']rng_start = [i for i in sh.range("B:B").value if i != None]  # 单元格内容rng_end = [i for i in sh.range("C:C").value if i != None]  # 单元格内容j = sh.range('a1').expand('table').rows.count  # 序号for i in range(len(rng_start) - 1):try:# for i in range(len(location)):# df_a = data_a(location[i], location[i + 1])  # 执行查询df_a = data_a(rng_start[i + 1], rng_end[i + 1])  # 执行查询df = pd.concat([df_a, df], axis=0)  # 加入基础数据列df.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")df.to_csv("distance.csv", mode="a+", header=None, index=None, encoding="utf-8-sig", sep=',')except:print('查询错误')# 距离写入Jason# df.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")# 读取jsonwith open("distance.json", 'r', encoding='utf-8') as f:data = json.load(f)print(data)app.display_alerts = Falseapp.screen_updating = Falsefor i in range(len(data)):  # 写入数据try:sh.cells[i + 1, 0].value = i + 1sh.cells[i + 1, 1].value = data[i]["start"]sh.cells[i + 1, 2].value = data[i]["end"]sh.cells[i + 1, 3].value = data[i]["距离"]except:print('写入错误')try:wb.save('distance.xlsx')wb.close()app.quit()# 获得当前窗口句柄sreach_windows = driver.current_window_handledriver.quit()# 获得当前所有打开的窗口的句柄all_handles = driver.window_handlesfor handle in all_handles:driver.switch_to.window(handle)driver.close()except:print('保存异常')# print(html)
# html.list = html.find_all('div', attrs={'class': 'sckrox'})
# print(html.list)# str=['中国铝业','中国核电','中国']
# print(str[1])# driver.find_element_by_id().send_keys()
# driver.find_element_by_name()
# driver.find_elements_by_class_name()
# https://map.baidu.com/dir/%E4%B8%8A%E6%B5%B7%E5%B8%82/%E6%9D%AD%E5%B7%9E%E5%B8%82/@13409603.494459692,3577628.985,10.23z?querytype=nav&c=29&sn=0$$8eb697c2bdf8bae8b6f42fe7$$13523308,3641053$$%E4%B8%8A%E6%B5%B7%E5%B8%82$$0$$$$&en=0$$99f429f6963d1f271fe999e7$$13382445,3514415$$%E6%9D%AD%E5%B7%9E%E5%B8%82$$0$$$$&sc=29&ec=29&pn=0&rn=5&mrs=1&version=4&route_traffic=1&sy=0&da_src=shareurl')
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='sole-searchbox-content']/div[@class='searchbox-content-button right-button route-button loading-button']").click()
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='route-header']/div[@class='searchbox-content-common route-tabs']/div[@class='tab-item walk-tab']/span").click()

百度地图测距:

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
from selenium.webdriver import Chrome, ChromeOptions
import pandas as pd
import jsondef data_a(start, end):df = pd.DataFrame()distance = []# 清空数据driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/input').clear()driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/input').clear()driver.find_element_by_xpath("//div['routebox-input route-start']/input[@class='route-start-input']").send_keys(start)driver.find_element_by_xpath("//div[@class='routebox-input route-end']/input[@class='route-end-input']").send_keys(end)driver.find_element_by_id('search-button').click()  # 模拟点击html = BeautifulSoup(driver.page_source, 'html.parser')time.sleep(3)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")with open('rr.html', 'w', encoding='utf-8') as f:  # 写入网页f.write(source)# time.sleep(3)# with open('rr.html', 'r', encoding='utf-8') as f:#打开网页#     html_a = BeautifulSoup(f, 'lxml')# 获取距离html.list = html.find_all('div', attrs={'class': 'navtrans-navlist-title'})for i, item in enumerate(html.list):try:distance.append(item.find_all('span')[2].text.strip('公里'))# print(item.find_all('span')[2].text.strip('公里'),)df["start"] = start,df["end"] = end,df["距离"] = (distance[0]),# print(df)except:print(str(i), "网页获取错误")return dfdf = pd.DataFrame()
if __name__ == "__main__":opt = ChromeOptions()  # 创建Chrome参数对象opt.headless = False  # 把Chrome设置成可视化无界面模式,windows/Linux 皆可driver = Chrome(options=opt)# driver = webdriver.Chrome()driver.get('https://map.baidu.com/')driver.find_element_by_xpath("//div[@class='searchbox-content-button right-button route-button loading-button']").click()  # 点击路线导航driver.find_element_by_xpath("//div[@class='searchbox-content-common route-tabs']/div[@class='tab-item drive-tab']/span").click()  # 点击驾车导航location = ['北京', '上海', '深圳', '合肥', '杭州']app = xw.App(visible=True, add_book=False)wb = app.books.open('distance.xlsx')# 创建一个worksheetsh = wb.sheets['distance']rng_start = [i for i in sh.range("B:B").value if i != None]  # 单元格内容rng_end = [i for i in sh.range("C:C").value if i != None]  # 单元格内容j = sh.range('a1').expand('table').rows.count  # 序号for i in range(len(rng_start) - 1):try:# for i in range(len(location)):# df_a = data_a(location[i], location[i + 1])  # 执行查询df_a = data_a(rng_start[i + 1], rng_end[i + 1])  # 执行查询df = pd.concat([df_a, df], axis=0)  # 加入基础数据列except:print('查询错误')# 距离写入Jasondf.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")# 读取jsonwith open("distance.json", 'r', encoding='utf-8') as f:data = json.load(f)print(data)app.display_alerts = Falseapp.screen_updating = Falsefor i in range(len(data)):  # 写入数据try:sh.cells[i + 1, 0].value = i + 1sh.cells[i + 1, 1].value = data[i]["start"]sh.cells[i + 1, 2].value = data[i]["end"]sh.cells[i + 1, 3].value = data[i]["距离"]except:print('写入错误')try:wb.save('distance.xlsx')wb.close()app.quit()# 获得当前窗口句柄sreach_windows = driver.current_window_handledriver.quit()# 获得当前所有打开的窗口的句柄all_handles = driver.window_handlesfor handle in all_handles:driver.switch_to.window(handle)driver.close()except:print('保存异常')# print(html)
# html.list = html.find_all('div', attrs={'class': 'sckrox'})
# print(html.list)# str=['中国铝业','中国核电','中国']
# print(str[1])# driver.find_element_by_id().send_keys()
# driver.find_element_by_name()
# driver.find_elements_by_class_name()
# https://map.baidu.com/dir/%E4%B8%8A%E6%B5%B7%E5%B8%82/%E6%9D%AD%E5%B7%9E%E5%B8%82/@13409603.494459692,3577628.985,10.23z?querytype=nav&c=29&sn=0$$8eb697c2bdf8bae8b6f42fe7$$13523308,3641053$$%E4%B8%8A%E6%B5%B7%E5%B8%82$$0$$$$&en=0$$99f429f6963d1f271fe999e7$$13382445,3514415$$%E6%9D%AD%E5%B7%9E%E5%B8%82$$0$$$$&sc=29&ec=29&pn=0&rn=5&mrs=1&version=4&route_traffic=1&sy=0&da_src=shareurl')
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='sole-searchbox-content']/div[@class='searchbox-content-button right-button route-button loading-button']").click()
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='route-header']/div[@class='searchbox-content-common route-tabs']/div[@class='tab-item walk-tab']/span").click()

python中支持库发模式,即点击起点和自动城市模式:

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
from selenium.webdriver import Chrome, ChromeOptions
import pandas as pd
import jsondef data_a(start, end):df = pd.DataFrame()distance = []# 清空数据try:driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/input').clear()driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/input').clear()driver.find_element_by_xpath("//div['routebox-input route-start']/input[@class='route-start-input']").send_keys(start)driver.find_element_by_xpath("//div[@class='routebox-input route-end']/input[@class='route-end-input']").send_keys(end)time.sleep(1.5)driver.find_element_by_id('search-button').click()  # 模拟点击time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_0_0']").click()  # 点击起点time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()#点击起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()  # 点击起点html = BeautifulSoup(driver.page_source, 'html.parser')time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")except:print("点击错误")html = BeautifulSoup(driver.page_source, 'html.parser')time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")with open('rr.html', 'w', encoding='utf-8') as f:  # 写入网页f.write(source)# time.sleep(1)# with open('rr.html', 'r', encoding='utf-8') as f:#打开网页#     html_a = BeautifulSoup(f, 'lxml')# 获取距离html.list = html.find_all('div', attrs={'class': 'navtrans-navlist-title'})for i, item in enumerate(html.list):try:distance.append(item.find_all('span')[2].text.strip('公里'))# print(item.find_all('span')[2].text.strip('公里'),)df["start"] = start,df["end"] = end,df["距离"] = (distance[0]),# print(df)except:print(str(i), "网页获取错误")return dfdf = pd.DataFrame()
if __name__ == "__main__":opt = ChromeOptions()  # 创建Chrome参数对象opt.headless = False  # 把Chrome设置成可视化无界面模式,windows/Linux 皆可driver = Chrome(options=opt)# driver = webdriver.Chrome()driver.get('https://map.baidu.com/')driver.find_element_by_xpath("//div[@class='searchbox-content-button right-button route-button loading-button']").click()  # 点击路线导航driver.find_element_by_xpath("//div[@class='searchbox-content-common route-tabs']/div[@class='tab-item drive-tab']/span").click()  # 点击驾车导航location = ['北京', '上海', '深圳', '合肥', '杭州']app = xw.App(visible=False, add_book=False)wb = app.books.open('distance.xlsx')# 创建一个worksheetsh = wb.sheets['distance']rng_start = [i for i in sh.range("B:B").value if i != None]  # 单元格内容rng_end = [i for i in sh.range("C:C").value if i != None]  # 单元格内容j = sh.range('a1').expand('table').rows.count  # 序号for i in range(len(rng_start) - 1):try:# for i in range(len(location)):# df_a = data_a(location[i], location[i + 1])  # 执行查询df_a = data_a(rng_start[i + 1], rng_end[i + 1])  # 执行查询df = pd.concat([df_a, df], axis=0)  # 加入基础数据列except:print('查询错误')# 距离写入Jasondf.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")# 读取jsonwith open("distance.json", 'r', encoding='utf-8') as f:data = json.load(f)print(data)app.display_alerts = Falseapp.screen_updating = Falsefor i in range(len(data)):  # 写入数据try:sh.cells[i + 1, 0].value = i + 1sh.cells[i + 1, 1].value = data[i]["start"]sh.cells[i + 1, 2].value = data[i]["end"]sh.cells[i + 1, 3].value = data[i]["距离"]except:print('写入错误')try:wb.save('distance.xlsx')wb.close()app.quit()# 获得当前窗口句柄sreach_windows = driver.current_window_handledriver.quit()# 获得当前所有打开的窗口的句柄all_handles = driver.window_handlesfor handle in all_handles:driver.switch_to.window(handle)driver.close()except:print('保存异常')# print(html)
# html.list = html.find_all('div', attrs={'class': 'sckrox'})
# print(html.list)# str=['中国铝业','中国核电','中国']
# print(str[1])# driver.find_element_by_id().send_keys()
# driver.find_element_by_name()
# driver.find_elements_by_class_name()
# https://map.baidu.com/dir/%E4%B8%8A%E6%B5%B7%E5%B8%82/%E6%9D%AD%E5%B7%9E%E5%B8%82/@13409603.494459692,3577628.985,10.23z?querytype=nav&c=29&sn=0$$8eb697c2bdf8bae8b6f42fe7$$13523308,3641053$$%E4%B8%8A%E6%B5%B7%E5%B8%82$$0$$$$&en=0$$99f429f6963d1f271fe999e7$$13382445,3514415$$%E6%9D%AD%E5%B7%9E%E5%B8%82$$0$$$$&sc=29&ec=29&pn=0&rn=5&mrs=1&version=4&route_traffic=1&sy=0&da_src=shareurl')
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='sole-searchbox-content']/div[@class='searchbox-content-button right-button route-button loading-button']").click()
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='route-header']/div[@class='searchbox-content-common route-tabs']/div[@class='tab-item walk-tab']/span").click()

python中支持点击单独目的地终点:

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
from selenium.webdriver import Chrome, ChromeOptions
import pandas as pd
import jsondef data_a(start, end):df = pd.DataFrame()distance = []# 清空数据try:driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/input').clear()driver.find_element_by_xpath('//html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/input').clear()driver.find_element_by_xpath("//div['routebox-input route-start']/input[@class='route-start-input']").send_keys(start)driver.find_element_by_xpath("//div[@class='routebox-input route-end']/input[@class='route-end-input']").send_keys(end)time.sleep(1.5)driver.find_element_by_id('search-button').click()  # 模拟点击time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_0_0']").click()  # 点击起点time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()#点击起点# driver.find_element_by_xpath("//tbody/tr/td[2]/div[@class='selBtn']").click()  # 点击起点html = BeautifulSoup(driver.page_source, 'html.parser')time.sleep(2)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")except:try:time.sleep(2)driver.find_element_by_xpath("//tbody/tr/td[2]/div[@tid='selBtn_1_0']").click()  # 点击起点except:print("点击错误")html = BeautifulSoup(driver.page_source, 'html.parser')# time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")print("点击错误")html = BeautifulSoup(driver.page_source, 'html.parser')# time.sleep(1.5)source = driver.page_sourcehtml = BeautifulSoup(source, "html.parser")with open('rr.html', 'w', encoding='utf-8') as f:  # 写入网页f.write(source)# time.sleep(1)# with open('rr.html', 'r', encoding='utf-8') as f:#打开网页#     html_a = BeautifulSoup(f, 'lxml')# 获取距离html.list = html.find_all('div', attrs={'class': 'navtrans-navlist-title'})for i, item in enumerate(html.list):try:distance.append(item.find_all('span')[2].text.strip('公里'))# print(item.find_all('span')[2].text.strip('公里'),)df["start"] = start,df["end"] = end,df["距离"] = (distance[0]),# print(df)except:print(str(i), "网页获取错误")return dfdf = pd.DataFrame()
if __name__ == "__main__":opt = ChromeOptions()  # 创建Chrome参数对象opt.headless = False  # 把Chrome设置成可视化无界面模式,windows/Linux 皆可driver = Chrome(options=opt)# driver = webdriver.Chrome()driver.get('https://map.baidu.com/')driver.find_element_by_xpath("//div[@class='searchbox-content-button right-button route-button loading-button']").click()  # 点击路线导航driver.find_element_by_xpath("//div[@class='searchbox-content-common route-tabs']/div[@class='tab-item drive-tab']/span").click()  # 点击驾车导航location = ['北京', '上海', '深圳', '合肥', '杭州']app = xw.App(visible=False, add_book=False)wb = app.books.open('distance.xlsx')# 创建一个worksheetsh = wb.sheets['distance']rng_start = [i for i in sh.range("B:B").value if i != None]  # 单元格内容rng_end = [i for i in sh.range("C:C").value if i != None]  # 单元格内容j = sh.range('a1').expand('table').rows.count  # 序号for i in range(len(rng_start) - 1):try:# for i in range(len(location)):# df_a = data_a(location[i], location[i + 1])  # 执行查询df_a = data_a(rng_start[i + 1], rng_end[i + 1])  # 执行查询df = pd.concat([df_a, df], axis=0)  # 加入基础数据列except:print('查询错误')# 距离写入Jasondf.to_json('distance.json', orient='records', indent=1, force_ascii=False)  # ,orient="values")# 读取jsonwith open("distance.json", 'r', encoding='utf-8') as f:data = json.load(f)print(data)app.display_alerts = Falseapp.screen_updating = Falsefor i in range(len(data)):  # 写入数据try:sh.cells[i + 1, 0].value = i + 1sh.cells[i + 1, 1].value = data[i]["start"]sh.cells[i + 1, 2].value = data[i]["end"]sh.cells[i + 1, 3].value = data[i]["距离"]except:print('写入错误')try:wb.save('distance.xlsx')wb.close()app.quit()# 获得当前窗口句柄sreach_windows = driver.current_window_handledriver.quit()# 获得当前所有打开的窗口的句柄all_handles = driver.window_handlesfor handle in all_handles:driver.switch_to.window(handle)driver.close()except:print('保存异常')# print(html)
# html.list = html.find_all('div', attrs={'class': 'sckrox'})
# print(html.list)# str=['中国铝业','中国核电','中国']
# print(str[1])# driver.find_element_by_id().send_keys()
# driver.find_element_by_name()
# driver.find_elements_by_class_name()
# https://map.baidu.com/dir/%E4%B8%8A%E6%B5%B7%E5%B8%82/%E6%9D%AD%E5%B7%9E%E5%B8%82/@13409603.494459692,3577628.985,10.23z?querytype=nav&c=29&sn=0$$8eb697c2bdf8bae8b6f42fe7$$13523308,3641053$$%E4%B8%8A%E6%B5%B7%E5%B8%82$$0$$$$&en=0$$99f429f6963d1f271fe999e7$$13382445,3514415$$%E6%9D%AD%E5%B7%9E%E5%B8%82$$0$$$$&sc=29&ec=29&pn=0&rn=5&mrs=1&version=4&route_traffic=1&sy=0&da_src=shareurl')
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='sole-searchbox-content']/div[@class='searchbox-content-button right-button route-button loading-button']").click()
# driver.find_element_by_xpath(
#     "/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='route-header']/div[@class='searchbox-content-common route-tabs']/div[@class='tab-item walk-tab']/span").click()

python百度地图测距测试:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import re# vertion 1.0
driver = webdriver.Chrome()
driver.get('https://map.baidu.com/')driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='sole-searchbox-content']/div[@class='searchbox-content-button right-button route-button loading-button']").click()
driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='route-header']/div[@class='searchbox-content-common route-tabs']/div[@class='tab-item walk-tab']/span").click()# 单击第一个按钮进入搜索界面def startSpide(start, end):# start是第一输入框的内容,end是第二个,startSpide爬两地点间时间文字信息driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/input').clear()driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/input').clear()# 清空两个输入框,即问号按键driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='routebox']/div[@class='searchbox-content-common routebox-content']/div[@class='routebox-inputs']/div[@class='routebox-input route-start']/input[@class='route-start-input']").send_keys(start)driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/div[@id='searchbox-container']/div[@id='route-searchbox-content']/div[@class='routebox']/div[@class='searchbox-content-common routebox-content']/div[@class='routebox-inputs']/div[@class='routebox-input route-end']/input[@class='route-end-input']").send_keys(end)# 在两个框内输入字符start和enddriver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/button[@id='search-button']").click()# 点击搜索time.sleep(2)try:driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@class='RouteAddressOuterBkg']/div[@class='RouteAddressInnerBkg']/div[@id='RouteAddress_DIV1']/div[@class='sel_body']/div[@id='RADIV_BODY1']/div[@id='RADiv_ResItem1']/div[@id='RA_ResItem_1']/table/tbody/tr[1]/td[2]").click()except:a = 1else:time.sleep(2)try:driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@class='RouteAddressOuterBkg']/div[@class='RouteAddressInnerBkg']/div[@id='RouteAddress_DIV0']/div[@class='sel_body']/div[@id='RADIV_BODY0']/div[@id='RADiv_ResItem0']/div[@id='RA_ResItem_0']/table/tbody/tr[1]/td[2]").click()except:a = 1# 出现模糊地址的时候选择第一个准确地址time.sleep(2)try:  # 超时异常处理WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.XPATH,"/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@id='nav_container']/div[@class='route_contentWrapper walk_cont']/div[@id='route_content_walk']/div[@class='special']/div[@class='drive']/div[@class='con']/div[@class='navtrans-navlist-view active expand navtrans-type-walk']/div[@class='navtrans-navlist-title']/p[@class='navtrans-navlist-title-p title-info']/span[@class='last']")))except:driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/div[@id='searchbox']/button[@id='search-button']").click()try:driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@class='RouteAddressOuterBkg']/div[@class='RouteAddressInnerBkg']/div[@id='RouteAddress_DIV1']/div[@class='sel_body']/div[@id='RADIV_BODY1']/div[@id='RADiv_ResItem1']/div[@id='RA_ResItem_1']/table/tbody/tr[1]/td[2]").click()except:a = 1time.sleep(2)try:driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@class='RouteAddressOuterBkg']/div[@class='RouteAddressInnerBkg']/div[@id='RouteAddress_DIV0']/div[@class='sel_body']/div[@id='RADIV_BODY0']/div[@id='RADiv_ResItem0']/div[@id='RA_ResItem_0']/table/tbody/tr[1]/td[2]").click()except:a = 1try:span2 = driver.find_element_by_xpath("/html/body[@class='pc']/div[@id='app']/div[@id='left-panel']/ul[@id='cards-level1']/li[1]/div[@id='nav_container']/div[@class='route_contentWrapper walk_cont']/div[@id='route_content_walk']/div[@class='special']/div[@class='drive']/div[@class='con']/div[@class='navtrans-navlist-view active expand navtrans-type-walk']/div[@class='navtrans-navlist-title']/p[@class='navtrans-navlist-title-p title-info']/span[@class='last']").get_attribute('textContent')except:span2 = '-1'# 找到文字的信息span2 = transform(span2)print(span2, end=" ")def transform(span):# 把文字转换为以米为单位的数字if span.find("公里") != -1:span = re.sub('公里', '', span)span = re.sub('约', '', span)span = int(float(span) * 1000)else:span = re.sub('约', '', span)span = re.sub('米', '', span)span = int(float(span))return spanlocation = ["中山大学(东校区)-牌坊", "中山大学(东校区)-软件学院", "中山大学东校区-国家超级计算广州中心", "中山大学东校区行政会议中心-A座", "中山大学(东校区)-停车场","中山大学(东校区)-行政会议中心", "中山大学(东校区)公共教学楼-A座", "中山大学(东校区)行政会议中心-B座", "中山大学北学院楼", \"默园", "中山大学东校区-公共教学楼E", "三行情书林", "中山大学(东校区)-图书馆", "落雁台", "广州东校区教学实验中心-A座", "广州东校区教学实验中心-D座", \"中山大学(东校区)-传播与设计学院", "中山大学(东校区)-体育馆", "中山大学(东校区)-东田径场", "中山大学(东校区)-篮球场", "中山大学(东校区)-游泳池", "广州东校区西田径场","中山大学-工学院校友回望林", \"中山大学东校区学生第一食堂", "至善学生活动中心", "格致园", "至善园", '慎思园', '明德园', '中山大学东校区学生公寓']# for i in range(len(location)):
for j in range(len(location)):if (j == 10):print(0, end=" ")else:startSpide(location[10], location[j])
print('')

python中地图测距相关推荐

  1. 【python】百度地图测距

    假设现在有一批地址需要知道他们之间的对应距离,百度地图网页测距便提供了这样的功能,实现方式也很简单,分别找到这两个点,点测距工具就行,但现在我们有很多的地址数据,为了提高效率,借助python也可以快 ...

  2. Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置

    1 任务需求   首先,我们来明确一下本文所需实现的需求.   现有通过这篇博客(https://blog.csdn.net/zhebushibiaoshifu/article/details/123 ...

  3. python软件是干什么用的-python中的django是做什么的

    Django是什么? Django是一个基于Python的Web应用框架.它与Python的另外一个Web 框架 Flask最大的区别是,它奉行 "包含一切" 的哲学.该理念即为: ...

  4. python 城市地图_python – 使用Basemap获取城市地图的最佳方式?

    我正在尝试使用Basemap在 python中显示城市地图,例如旧金山.我尝试过以下方法: from mpl_toolkits.basemap import Basemap import numpy ...

  5. 在Python中操纵json数据的最佳方式

    ❝ 本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes ❞ 1 简介 在日常使用Python的过程中,我 ...

  6. Python中的注释和算数运算符

    Python中的注释和算数运算符 文章目录 Python中的注释和算数运算符 一.注释 01. 注释的作用 02. 单行注释(行注释) 在代码后面增加的单行注释 03. 多行注释(块注释) 什么时候需 ...

  7. Python中的线性回归:Sklearn与Excel

    内部AI (Inside AI) Around 13 years ago, Scikit-learn development started as a part of Google Summer of ...

  8. 在Python中有效使用JSON的4个技巧

    Python has two data types that, together, form the perfect tool for working with JSON: dictionaries ...

  9. 如何使用Elasticsearch,Logstash和Kibana实时可视化Python中的日志

    by Ritvik Khanna Ritvik Khanna着 如何使用Elasticsearch,Logstash和Kibana实时可视化Python中的日志 (How to use Elastic ...

最新文章

  1. java common http_httpClient和common-httpclient的区别
  2. 凌琦:物联网——一场正在发生的变革
  3. as无效 mysql_mysql 排它锁无效
  4. linux语言windows 语言,作业系统一般用什么编码语言程式设计?如:Windows,Linux,是组合语言吗?还是自己开发的程式码?...
  5. 揭秘下一代云数据库引擎MyBasefor PostgreSQL
  6. 计算机三种引用方式,单元格的引用方式有哪几种
  7. Python学习笔记——GIF倒放处理
  8. python画锯齿波_用Python控制硬件35-自制二三十元成本的信号测量采集控制系统
  9. What’s the difference between system.web and system.webServer?
  10. 没有文化可以学php吗,宋威龙深夜发文感慨,却被吐槽文化低,学历真的重要吗?...
  11. 《见缝插针》游戏开发记录
  12. 修改Office 2016安装路径的方法
  13. python 正数变成负数_LeetCode 007:整数反转 (Python)
  14. hive编程指南笔记
  15. cisco 路由器时间戳service timestamps
  16. 计算机组装维护看图填空,电脑组装测试维护故障排除
  17. usb扩展坞同时接键盘鼠标_一种带有扩展坞功能的一体式键鼠的制作方法
  18. 电商图片采集-免费批量电商图片采集软件
  19. python机器学习 | SVM算法介绍及实现
  20. =logistic求导

热门文章

  1. 18周岁以下签订租赁合同有效吗?租赁合同签订日期规定是怎样的?
  2. 理财笔记 - 老猫有感
  3. Java 碰壁小球游戏实例教程
  4. GifCam2.0使用
  5. 怎样把其他格式的音频文件转换成MP3格式的
  6. 一键U盘启动快捷方式
  7. QtCreator代码对齐的方法
  8. 深入浅出Java开发!什么是分布式系统,如何学习分布式系统
  9. maya要学python吗_Day1 为什么要学Python?
  10. js数据过滤算法搭建