编辑
2024-09-17
男主人
00
请注意,本文编写于 269 天前,最后修改于 269 天前,其中某些信息可能已经过时。

目录

需求:客户使用我们软件的时候能比较精准的定位使用地点,从而管控软件使用位置。
困难:用户设备为普通电脑主机,没有wifi蓝牙等,地图类API提供的接口针对这种情况只能返回通过IP定位的大概地址。
大致思路:
步骤1:安装必要的库
步骤2:下载浏览器驱动
步骤3:编写Python脚本
步骤4:打包成可执行文件
步骤5:运行可执行文件
注意事项
初级版本的完整代码:
接下来想要实现

需求:客户使用我们软件的时候能比较精准的定位使用地点,从而管控软件使用位置。

困难:用户设备为普通电脑主机,没有wifi蓝牙等,地图类API提供的接口针对这种情况只能返回通过IP定位的大概地址。

解决方案1、带U盘功能的无线网卡,通过无线网卡获得wifi信息,再通过地图类api,分析wifi信息获得较为精准的地址。

解决方案2、发现百度地图自己的web定位非常准,咨询官方客服答复他们自己的定位方式不开放。 想用些笨办法硬来。

大致思路:

要创建一个包含浏览器内核的Python应用程序,并在任何电脑上运行以获取百度地图网页中的当前位置,你可以使用PyInstaller将Python脚本打包成独立的可执行文件。以下是一个完整的示例,使用Selenium进行浏览器自动化,并通过PyInstaller打包成可执行文件。

步骤1:安装必要的库

首先,确保你已经安装了以下Python库:

  • Selenium
  • PyInstaller

你可以使用以下命令安装这些库:

bash
pip install selenium pyinstaller

步骤2:下载浏览器驱动

确保你下载了与你的浏览器版本匹配的浏览器驱动程序(如ChromeDriver)。你可以从ChromeDriver官网下载。

步骤3:编写Python脚本

以下是一个示例Python脚本,它会自动访问百度地图,点击“我的位置”按钮,处理权限对话框,并获取当前位置的详细信息。

python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time def get_current_location(): # 设置浏览器选项 options = webdriver.ChromeOptions() options.add_argument("--start-maximized") # 初始化WebDriver driver = webdriver.Chrome(options=options) try: # 打开百度地图 driver.get("https://map.baidu.com/") # 等待页面加载 time.sleep(5) # 点击“我的位置”按钮 my_location_button = driver.find_element(By.CSS_SELECTOR, ".widget-mycenter") my_location_button.click() # 等待权限对话框并点击允许 time.sleep(5) alert = driver.switch_to.alert alert.accept() # 等待定位完成并点击中心位置 time.sleep(10) center_location = driver.find_element(By.CSS_SELECTOR, ".BMap_Marker") ActionChains(driver).move_to_element(center_location).click().perform() # 获取位置信息 time.sleep(5) location_info = driver.find_element(By.CSS_SELECTOR, ".BMap_bubble_content") print(location_info.text) finally: # 关闭浏览器 driver.quit() if __name__ == "__main__": get_current_location()

步骤4:打包成可执行文件

使用PyInstaller将Python脚本打包成独立的可执行文件。

bash
pyinstaller --onefile --add-binary "path/to/chromedriver;." your_script.py

其中,path/to/chromedriver是你下载的ChromeDriver的路径,your_script.py是你的Python脚本的文件名。

步骤5:运行可执行文件

在打包完成后,你可以在dist目录下找到生成的可执行文件。将其复制到任何电脑上运行,即可查看当前位置。

注意事项

  1. 浏览器驱动:确保ChromeDriver与Chrome浏览器版本匹配。如果需要支持多个浏览器,可以下载相应的驱动程序并进行配置。
  2. 权限对话框:不同操作系统和浏览器可能会有不同的权限对话框处理方式,必要时需要进行调整。
  3. 定位时间:定位需要一些时间,确保适当的等待时间,以便获取准确的位置信息。

通过上述步骤,你可以创建一个包含浏览器内核的Python应用程序,并在任何电脑上运行以获取百度地图网页中的当前位置。

基本这个思路就能实现了,具体的里面精准控制点击的元素就靠F12一个个对了。selenium挺牛

初级版本的完整代码:

python
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def get_full_address(): # 设置浏览器选项 options = webdriver.ChromeOptions() options.add_argument("--start-maximized") # 初始化WebDriver driver = webdriver.Chrome(options=options) try: # 打开百度地图 driver.get("https://map.baidu.com/") # 等待页面加载 WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "iploc")) ) # 使用开发者工具找到正确的按钮选择器 my_location_button = driver.find_element(By.ID, "iploc") # 点击“我的位置”按钮 my_location_button.click() # 等待权限对话框出现并点击“确定”按钮 WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "allowLocationCard")) ) allow_button = driver.find_element(By.CSS_SELECTOR, ".sure-btn.sureLoc-btn.locbtn") allow_button.click() # 增加等待时间,确保地址加载完成 time.sleep(3) # 等待并获取第一个地址 WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".ipLocTitle strong")) ) first_address_element = driver.find_element(By.CSS_SELECTOR, ".ipLocTitle strong") first_address = first_address_element.text.strip() # 点击第一个地址的按钮 first_address_element.click() # 增加等待时间,确保对话框加载完成 time.sleep(2) # 等待并获取第二个地址 WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".addr-info")) ) second_address_element = driver.find_element(By.CSS_SELECTOR, ".addr-info") second_address = second_address_element.text.strip() # 拼接地址 full_address = f"{second_address}, {first_address}" print(full_address) finally: # 关闭浏览器 driver.quit() if __name__ == "__main__": get_full_address()

接下来想要实现

接下来我想要打包成一个exe文件,让他在任何Windows电脑上都能运行,即使其他电脑没有chrome浏览器。 控制台显示地址的时候不要显示一会儿就自动关闭,让他一直显示着。

注意打包的时候的路径问题,不然换个电脑,路径不对就不好使了

Python
import os import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service def get_full_address(): try: print("Setting up browser options...") # 获取当前脚本所在目录 current_dir = os.path.dirname(os.path.abspath(__file__)) # 设置浏览器选项 options = webdriver.ChromeOptions() options.add_argument("--start-maximized") options.add_argument("--headless") # 使用无头模式 options.add_argument("--disable-gpu") # 使用相对路径设置Chromium路径 options.binary_location = os.path.join(current_dir, "Chrome-bin", "chrome.exe") # 使用相对路径设置chromedriver路径 chromedriver_path = os.path.join(current_dir, "chromedriver.exe") # 创建Service对象 service = Service(executable_path=chromedriver_path) # 初始化WebDriver print("Initializing WebDriver...") driver = webdriver.Chrome(service=service, options=options) try: # 打开百度地图 print("Opening Baidu Maps...") driver.get("https://map.baidu.com/") # 等待页面加载 print("Waiting for page to load...") WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "iploc")) ) # 使用开发者工具找到正确的按钮选择器 print("Finding 'My Location' button...") my_location_button = driver.find_element(By.ID, "iploc") # 点击“我的位置”按钮 print("Clicking 'My Location' button...") my_location_button.click() # 等待权限对话框出现并点击“确定”按钮 print("Waiting for permission dialog...") WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "allowLocationCard")) ) allow_button = driver.find_element(By.CSS_SELECTOR, ".sure-btn.sureLoc-btn.locbtn") print("Clicking 'Allow' button...") allow_button.click() # 增加等待时间,确保地址加载完成 time.sleep(3) # 等待并获取第一个地址 print("Waiting for first address...") WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".ipLocTitle strong")) ) first_address_element = driver.find_element(By.CSS_SELECTOR, ".ipLocTitle strong") first_address = first_address_element.text.strip() # 点击第一个地址的按钮 print("Clicking first address...") first_address_element.click() # 增加等待时间,确保对话框加载完成 time.sleep(2) # 等待并获取第二个地址 print("Waiting for second address...") WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".addr-info")) ) second_address_element = driver.find_element(By.CSS_SELECTOR, ".addr-info") second_address = second_address_element.text.strip() # 拼接地址 full_address = f"{second_address}, {first_address}" print(full_address) finally: # 关闭浏览器 print("Quitting browser...") driver.quit() except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": get_full_address() input("Press Enter to exit...")

打包的命令:

pyinstaller my_script.spec

配置文件: 配置文件路径可以写死,不然配置报错。

# my_script.spec # -*- mode: python ; coding: utf-8 -*- import os block_cipher = None current_dir = r'C:\Users\Admin\Desktop\weizhi' # 硬编码当前目录 a = Analysis( ['your_script.py'], pathex=[current_dir], binaries=[ (os.path.join(current_dir, 'Chrome-bin', 'chrome.exe'), 'Chrome-bin'), (os.path.join(current_dir, 'chromedriver.exe'), '.') ], datas=[ (os.path.join(current_dir, 'Chrome-bin'), 'Chrome-bin') ], hiddenimports=[], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, ) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE( pyz, a.scripts, [], exclude_binaries=True, name='your_script', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=True, ) coll = COLLECT( exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=True, upx_exclude=[], name='your_script', )

成功,哈哈,这就是面向chatgpt编程; 希望百度地图不要随便改东西,网页版一般不随便改吧。

附带chrominum和驱动的官方地址: https://chromium.woolyss.com/#windows-64-bit https://googlechromelabs.github.io/chrome-for-testing/

注意要版本一直才能使用的

本文作者:jiangjiang

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!