Selenium 高德地图根据名称获取坐标

下载google浏览器对应的webdriver :

https://blog.csdn.net/weixin_41522164/article/details/82775735

 下载 browsermob-proxy 插件:

https://github.com/lightbody/browsermob-proxy

具体实现代码

import time
import pandas as pd
from selenium import webdriver
from browsermobproxy import Server
from selenium.webdriver.chrome.options import Options

server = Server(r'E:\code\000-render-tools\python-spider-master\subwaymap\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat')
server.start()
proxy = server.create_proxy()

chrome_options = Options()
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--proxy-server={0}'.format(proxy.proxy))

driver = webdriver.Chrome(chrome_options=chrome_options)
proxy.new_har("amap", options={'captureHeaders': True, 'captureContent': True})
driver.get('https://www.amap.com/')

# cross_lst = ['广顺北大街与广顺桥交叉口']

path  = './frm_roaditem20200806'
with open(path + '.csv', 'r',encoding='gbk') as rf:
    df = pd.read_csv(rf)
    cross_lst = df['name'].astype(str)

    for cross_name in cross_lst:
        print(cross_name)
        search_key = driver.find_element_by_id('searchipt')
        search_key.clear()
        search_key.send_keys(cross_name)
        driver.find_element_by_id("searchbtn").click()

        result = proxy.har
        # print(result)
        for entry in result['log']['entries']:
            _url = entry['request']['url']
            if "/service/regeo" in _url:
                print(_url)
                time.sleep(1)
                # _response = entry['response']
                # _content = _response['content']['text']
                # 获取接口返回内容
                # print(_content)

time.sleep(5)
# driver.quit()  # 退出浏览器

 

你可能感兴趣的:(Python)