解决方案1、带U盘功能的无线网卡,通过无线网卡获得wifi信息,再通过地图类api,分析wifi信息获得较为精准的地址。
解决方案2、发现百度地图自己的web定位非常准,咨询官方客服答复他们自己的定位方式不开放。 想用些笨办法硬来。
要创建一个包含浏览器内核的Python应用程序,并在任何电脑上运行以获取百度地图网页中的当前位置,你可以使用PyInstaller将Python脚本打包成独立的可执行文件。以下是一个完整的示例,使用Selenium进行浏览器自动化,并通过PyInstaller打包成可执行文件。
首先,确保你已经安装了以下Python库:
你可以使用以下命令安装这些库:
bashpip install selenium pyinstaller
确保你下载了与你的浏览器版本匹配的浏览器驱动程序(如ChromeDriver)。你可以从ChromeDriver官网下载。
以下是一个示例Python脚本,它会自动访问百度地图,点击“我的位置”按钮,处理权限对话框,并获取当前位置的详细信息。
pythonfrom 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()
使用PyInstaller将Python脚本打包成独立的可执行文件。
bashpyinstaller --onefile --add-binary "path/to/chromedriver;." your_script.py
其中,path/to/chromedriver
是你下载的ChromeDriver的路径,your_script.py
是你的Python脚本的文件名。
在打包完成后,你可以在dist目录下找到生成的可执行文件。将其复制到任何电脑上运行,即可查看当前位置。
通过上述步骤,你可以创建一个包含浏览器内核的Python应用程序,并在任何电脑上运行以获取百度地图网页中的当前位置。
基本这个思路就能实现了,具体的里面精准控制点击的元素就靠F12一个个对了。selenium挺牛
pythonimport 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浏览器。 控制台显示地址的时候不要显示一会儿就自动关闭,让他一直显示着。
注意打包的时候的路径问题,不然换个电脑,路径不对就不好使了
Pythonimport 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 许可协议。转载请注明出处!