使用Python的 Google Maps Selenium自动化
先决条件:使用Selenium的浏览器自动化
Selenium是一个强大的工具,用于通过程序控制 Web 浏览器。它适用于所有浏览器,适用于所有主要操作系统,其脚本是用各种语言编写的,例如Python、 Java、C# 等,我们将使用Python。可以使用以下命令安装它:
pip install selenium
在本文中,我们将了解如何通过获取一个地点的位置及其到另一个位置的交通详细信息,使用selenium自动执行 Google 地图搜索。
步骤 1)导入模块
Python3
# import required modules
from selenium import webdriver
from time import sleep
Python3
# assign url in the webdriver object
driver = webdriver.Chrome()
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
Python3
# search locations
def searchplace():
Place = driver.find_element_by_class_name("tactile-searchbox-input")
Place.send_keys("Tiruchirappalli")
Submit = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button")
Submit.click()
searchplace()
Python3
# get directions
def directions():
sleep(10)
directions = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button")
directions.click()
directions()
Python3
# find place
def find():
sleep(6)
find = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input")
find.send_keys("Tirunelveli")
sleep(2)
search = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]")
search.click()
find()
Python3
# get transportation details
def kilometers():
sleep(5)
Totalkilometers = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div")
print("Total Kilometers:", Totalkilometers.text)
sleep(5)
Bus = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]")
print("Bus Travel:", Bus.text)
sleep(7)
Train = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div")
print("Train Travel:", Train.text)
sleep(7)
kilometers()
Python3
# import required modules
from selenium import webdriver
from time import sleep
# assign url in the webdriver object
driver = webdriver.Chrome()
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
# search locations
def searchplace():
Place = driver.find_element_by_class_name("tactile-searchbox-input")
Place.send_keys("Tiruchirappalli")
Submit = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button")
Submit.click()
searchplace()
# get directions
def directions():
sleep(10)
directions = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button")
directions.click()
directions()
# find place
def find():
sleep(6)
find = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input")
find.send_keys("Tirunelveli")
sleep(2)
search = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]")
search.click()
find()
# get transportation details
def kilometers():
sleep(5)
Totalkilometers = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div")
print("Total Kilometers:", Totalkilometers.text)
sleep(5)
Bus = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]")
print("Bus Travel:", Bus.text)
sleep(7)
Train = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div")
print("Train Travel:", Train.text)
sleep(7)
kilometers()
步骤 2)在您下载的驱动程序变量中提及您的 chrome 驱动程序的路径。然后我们需要得到一个谷歌地图网站。
蟒蛇3
# assign url in the webdriver object
driver = webdriver.Chrome()
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
Step 3) Next step 在这个函数下声明一个函数,你需要检查谷歌地图网站上的搜索栏。并在变量位置复制类名。您需要将密钥发送到特定的 Web 元素。将您的目的地作为输入。在用于按下搜索按钮的提交变量中提供xpath值
蟒蛇3
# search locations
def searchplace():
Place = driver.find_element_by_class_name("tactile-searchbox-input")
Place.send_keys("Tiruchirappalli")
Submit = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button")
Submit.click()
searchplace()
步骤 4)声明一个名为方向的函数。在此函数下,您需要向方向按钮发送点击。从 Google 地图网站复制路线按钮的 X 路径值。并将值粘贴到变量中。
蟒蛇3
# get directions
def directions():
sleep(10)
directions = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button")
directions.click()
directions()
步骤 5)声明一个名为 find 的函数。在这个函数下,你需要创建一个变量,在这个变量中,你需要复制搜索栏值的xpath ,并将值粘贴到变量中。
接下来,将起点发送到特定的搜索框。并且您需要向搜索按钮发送一个点击按钮以遵循步骤 1的过程。
蟒蛇3
# find place
def find():
sleep(6)
find = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input")
find.send_keys("Tirunelveli")
sleep(2)
search = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]")
search.click()
find()
第 6 步)现在我们需要废弃必要的数据来完成我们的自动化过程。这里我们需要复制两地之间Total km的xpath值
这两个地方之间的巴士旅行时间和火车旅行时间。在这里,我使用 Web-Elements 从谷歌地图网站提取数据。
蟒蛇3
# get transportation details
def kilometers():
sleep(5)
Totalkilometers = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div")
print("Total Kilometers:", Totalkilometers.text)
sleep(5)
Bus = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]")
print("Bus Travel:", Bus.text)
sleep(7)
Train = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div")
print("Train Travel:", Train.text)
sleep(7)
kilometers()
以下是基于上述方法的完整程序:
蟒蛇3
# import required modules
from selenium import webdriver
from time import sleep
# assign url in the webdriver object
driver = webdriver.Chrome()
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
# search locations
def searchplace():
Place = driver.find_element_by_class_name("tactile-searchbox-input")
Place.send_keys("Tiruchirappalli")
Submit = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[1]/div[1]/div[2]/div[1]/button")
Submit.click()
searchplace()
# get directions
def directions():
sleep(10)
directions = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div/button")
directions.click()
directions()
# find place
def find():
sleep(6)
find = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/div/input")
find.send_keys("Tirunelveli")
sleep(2)
search = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[3]/div[1]/div[2]/div/div[3]/div[1]/div[1]/div[2]/button[1]")
search.click()
find()
# get transportation details
def kilometers():
sleep(5)
Totalkilometers = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[2]/div")
print("Total Kilometers:", Totalkilometers.text)
sleep(5)
Bus = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[1]/div[1]/div[1]/div[1]/div[1]/span[1]")
print("Bus Travel:", Bus.text)
sleep(7)
Train = driver.find_element_by_xpath(
"/html/body/jsl/div[3]/div[9]/div[7]/div/div[1]/div/div/div[5]/div[2]/div[1]/div[2]/div[1]/div")
print("Train Travel:", Train.text)
sleep(7)
kilometers()
输出: