利用selenium批量获取百度地图的地址搜索结果(免api)
因为百度api逆编码免费额度上限,故使用模拟点击方式批量获取地址名对应详细地址
(真黑啊,故意的吧)
参考:https://blog.csdn.net/weixin_62588253/article/details/135607667
待更新:1.目前输入形式仅为列表,为把原有园区内容批量前加地区名如太仓市,再df['园区名'] = '"' + df['园区名']+'",'
,转成列表再复制。
(注意此步,原先尝试直接使用遍历读取列中名称,后发现此步可能与后续selenium冲突,故直接使用列表方法)
天哪能想到直接加“”,我简直是个天才
2.返回数值过长,考虑储存为excel。
def getAddress(locations: list) -> list:
"""
输入待查询的地址列表,以列表形式返回每个地址在百度地图查询到的第一个地址
:param locations:地址列表
:return:列表形式的地址
"""
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
searched_arr = [] # 保存搜索到的内容
options = webdriver.ChromeOptions()
options.add_argument("-headless") # 不显示浏览器
driver = webdriver.Chrome() # 添加options就不显示,这里没添加
driver.get("https://map.baidu.com/@12713770.095,3547943.7200000007,19z")
print(locations)
for i in range(len(locations)):
time.sleep(0.3)
driver.find_element(By.ID, "sole-input").send_keys(Keys.CONTROL, "a") # 全选输入框
driver.find_element(By.ID, "sole-input").send_keys(Keys.BACKSPACE) # 删除全选的输入框内容,即上次输入的地址
driver.find_element(By.ID, "sole-input").send_keys(locations[i]) # 输入新的地址
driver.find_element(By.XPATH, '//*[@id="search-button"]').click() # 点击搜索
time.sleep(1) # 等待加载结果
# driver.switch_to.window(driver.current_window_handle) # 切换到最新的句柄
print(driver.current_url) # 查看当前url
driver.get(driver.current_url) # 跳转到当前的搜索结果页面
time.sleep(1)
# 能搜索到的地区用简化替代或者不简化
try:
place = driver.find_element(By.CLASS_NAME, 'poilist').find_element(By.TAG_NAME, 'li').find_element(By.XPATH,
'//*[@id="card-1"]/div/div[1]/ul/li/div[1]/div[3]/div[2]').text
print(place)
# 搜索不到的地区则名称不变
except Exception:
place = locations[i]
print(place)
searched_arr.append(place) # 保存百度地图搜索到的内容
driver.quit()
return searched_arr
# 定义地址列表
addresses = [
"太仓市宋乔集团·好古科技产业园",
"太仓市太仓市中心企业创业园",
"太仓市昆宏EPP EPS生产基地",
"太仓市太星工业园区",
"太仓市苏州捷利达科技工业园",
"太仓市新型建材产业园",
"太仓市中节能产业园",
"太仓市洙桥工业园",
"太仓市台资科技创新产业园",
"太仓市南联工业园",
"太仓市江苏省太仓高新技术产业开发区",
"太仓市庄西农场产业融合升级发展基地",
"太仓市民营科技园",
"太仓市浏河慧创未来创新中心",
"太仓市兵哥哥乐创园",
"太仓市苏隆实业园",
"太仓市半泾村电商创业孵化基地",
"太仓市上海三兄服饰有限公司生产园区",
"太仓市太仓浩达商贸物流园",
"太仓市太仓市科技文化产业园",
"太仓市川湾生态产业园",
"太仓市中国工业设计研究院智能制造(太仓)产业园",
"太仓市万方龙达木材园区",
"太仓市太仓高新技术产业开发区南区",
"太仓市半泾工业园",
"太仓市京东上海大件物流中心京东商城-太仓平安园区",
"太仓市江苏太仓岳王科技创新产业园",
"太仓市陆渡产业园",
"太仓市太仓现代农业园区",
"太仓市太仓港国家级开发区璜泾工业园",
"太仓市太仓市沙溪镇工业园",
"太仓市银港工业区",
"太仓市宝洁生产园",
"太仓市松南工业园",
"太仓市环天镜湖中央商务区",
"太仓市隆兴钢铁物流园",
"太仓市中化太仓化工产业园",
"太仓市电站村工业园区",
"太仓市太仓星药港第一工园",
"太仓市太仓星药港二期",
"太仓市耐克物流园",
"太仓市太仓中德创新园·航空产业园",
"太仓市太仓中德创新园先进制造产业园",
"太仓市太仓中德先进制造技术国际创新园(中德企业合作基地)",
"太仓市华东国际塑化城",
"太仓市明达物流园",
"太仓市苏州蓝谐服务外包产业园",
"太仓市苏州富田工业园",
"太仓市中昱科技园(半泾北路)",
"太仓市太仓市纤维新材料产业园",
"太仓市归庄长富工业园区",
"太仓市太仓图辉物流园",
"太仓市基创物流园",
"太仓市太仓生物港",
"太仓市DLJ太仓港物流园",
"太仓市苏州华彤工业园",
"太仓市太仓智汇谷·科技创新园",
"太仓市普凯太仓港物流园东区",
"太仓市七溪生物硅谷5区",
"太仓市嘉民太仓工业园",
"太仓市祥平实业智能产业园",
"太仓市鹿河联影工业区",
"太仓市乐歌太仓欧洲产业园",
"太仓市森茂汽车城",
"太仓市普洛斯太仓物流园",
"太仓市平谦国际工业园(平谦太仓现代产业园)",
"太仓市创新工业园",
"太仓市印溪科技创新产业园",
"太仓市浙申(苏州)工业园区",
"太仓市中欧先进制造产业园",
"太仓市雅鹿智慧物流园",
"太仓市苏州欣旺工业园",
"太仓市太仓欧美高新技术产业园",
"太仓市新建苏宁环上海电商产业园",
"太仓市苏州国信集团东润物流工业园",
"太仓市福城欧美工业园",
"太仓市朝亚太仓大上海数据中心园区",
"太仓市事达同泰(太仓)产业园",
"太仓市和泰国际物流园",
"太仓市太仓中德创新园仓能工业园",
"太仓市中建材光电产业园",
"太仓市东林联发科技产业园(东林联发创业园)",
"太仓市上海连成集团太仓工业园区",
"太仓市太仓市科技创业园(国家级科技企业孵化器)",
"太仓市联达工业区",
"太仓市福地欧美工业园",
"太仓市太仓欧美绿色制造产业园",
"太仓市布托科技园",
"太仓市太仓软件园",
"太仓市富桥工业园",
"太仓市富桥工业园3期",
"太仓市凯明科技工业园",
"太仓市太仓华信工业园",
"太仓市太仓锦琪工业园",
"太仓市宏亮工业园(天津路)",
"太仓市太仓市生物医药产业园",
"太仓市天镜湖未来科技谷",
"太仓市乐哥太仓产业园",
"太仓市中科智慧产研城",
"太仓市赛纳科创园",
"太仓市雅鹿村化纤纺织产业园",
"太仓市太仓市科技企业加速器园区B区",
"太仓市科轮工业园",
"太仓市太仓市科技产业园(良辅路)",
"太仓市DLJ太仓物流园",
"太仓市海美国际太仓科技产业园二期",
"太仓市苏州幸龙工业园",
"太仓市中菱工业园",
"太仓市富尔达工业园",
"太仓市北郊工业园",
"太仓市太仓市合兴产业园",
"太仓市瓯江产业园",
"太仓市苏州神威汽车科技园",
"太仓市禅寺路工业园",
"太仓市太仓国际物流城",
"太仓市港龙科技工业园",
"太仓市太仓市仓能欧美科技坊",
"太仓市全众智能装备科技园",
"太仓市七溪生物硅谷2区",
"太仓市太仓大学科技园(太仓科教新城)",
"太仓市亚秦锐纳基智造园",
"太仓市大成工业园(南京东路)",
"太仓市世纪互联新建太仓国际车联网大数据产业园",
"太仓市太仓市生物医药创业园",
"太仓市车视杰产业园(敬公科创园)",
"太仓市德国科技园",
"太仓市盛智科技园",
"太仓市斯尔特工业园",
"太仓市示范太仓产业园",
"太仓市威斯伯特产业园",
"太仓市金利达园区",
"太仓市苏州特航科技产业园",
"太仓市N-PARK(质卓)现代产业园",
"太仓市爱法贝产业园",
"太仓市太仓市科技产业园(北漳泾路)",
"太仓市上海阿美泰克苏州工业园",
"太仓市太仓新城房产工业园",
"太仓市江苏太仓港出口加工工业园",
"太仓市横沥工业小区",
"太仓市太胜工业一园",
"太仓市太仓市科技企业加速器园区",
"太仓市高阳工业园",
"太仓市双凤镇科创孵化园",
"太仓市中国·太仓德国留学生创业园",
"太仓市高特科技产业园",
"太仓市佳顺工业园",
"太仓市太仓市国际服务外包园",
"太仓市申氏工业园",
"太仓市达凯工业园",
"太仓市洋沙社区工业园三园",
"太仓市利民工业园",
"太仓市金建工业苑",
"太仓市天工设计工业园",
"太仓市载顺产业园",
"太仓市N-PARK久弘达产业园",
"太仓市苏州锦誉食品产业园",
"太仓市美凤工业园",
"太仓市太仓市中小企业创业园",
"太仓市洲盛工业园",
"太仓市创智谷人才创业中心",
"太仓市璜泾商务文化园",
]
# 调用函数获取地址
search_results = getAddress(addresses)
# 打印结果
print(search_results)
共有 0 条评论