通过兴趣点获取坐标
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
# 保存Cookies到文件(可选)
import pickle
import csv
from selenium.webdriver.support.wait import WebDriverWait
# 初始化浏览器
driver = webdriver.Chrome()
# 打开目标网页
driver.get("https://lbs.amap.com/tools/picker")
# 加载保存的Cookies
with open('cookies.pkl', 'rb') as file:
cookies = pickle.load(file)
for cookie in cookies:
driver.add_cookie(cookie)
# 刷新页面以应用Cookies
driver.refresh()
csvfile = r"D:\Programs\py\pythonProject\data\pdf\a.csv"
dataW=[]
#打开csv文件
with open(csvfile,mode='r',newline='') as file:
#创建csv读取器
csvR=csv.reader(file)
#遍历每一行,跳过第一行
for row in csvR:
namecom = row[1]
username = driver.find_element(By.ID, "txtSearch")
login_button = driver.find_element(By.CLASS_NAME, "btn-search")
txtCoordinate = driver.find_element(By.ID, "txtCoordinate")
# 填写
username.clear()
username.send_keys(namecom)
#按钮搜索
coord = txtCoordinate.get_attribute("value")
login_button.click()
#获取坐标
# 定义一个函数来检查值是否发生变化
def value_changed(element, initial_value):
return element.get_attribute("value") != initial_value
try:
# 等待<input>值发生变化
WebDriverWait(driver, 10).until(lambda driver: value_changed(txtCoordinate, coord))
coord = txtCoordinate.get_attribute("value")
# 继续执行后续操作
xy=coord.split(',')
x=xy[0]
y=xy[1]
rowW = row.copy()
rowW.append(x)
rowW.append(y)
dataW.append(rowW)
except TimeoutException:
# 如果超时,执行其他操作
print(row)
continue
finally:
continue
#打开csv文件以写入模式
csvfileW=r"D:\Programs\py\pythonProject\data\pdf\aXY.csv"
with open(csvfileW,mode='w',newline='') as fileW:
#创建csv写入器
csvW=csv.writer(fileW)
#写入数据
for row in dataW:
csvW.writerow(row)
userinput = input("> ")
# 关闭浏览器
driver.quit()
手动登录用户名密码再保存cookies
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
# 初始化浏览器
driver = webdriver.Chrome()
# 打开目标网站
driver.get("https://lbs.amap.com/tools/picker")
userinput = input("任意键继续")
# 手动登录并获取Cookies
cookies = driver.get_cookies()
# 保存Cookies到文件(可选)
import pickle
with open('cookies.pkl', 'wb') as file:
pickle.dump(cookies, file)
# 关闭浏览器
driver.quit()
userinput = input("任意键继续")
# 在后续脚本中加载Cookies
driver = webdriver.Chrome()
driver.get('https://lbs.amap.com/tools/picker')
# 加载保存的Cookies
with open('cookies.pkl', 'rb') as file:
cookies = pickle.load(file)
for cookie in cookies:
driver.add_cookie(cookie)
# 刷新页面以应用Cookies
driver.refresh()