用 selenium 从原始地址列表中获取城市名称

chhkpiq4  于 2023-01-20  发布在  其他
关注(0)|答案(2)|浏览(161)

我有一个地址列表list_x = ['A', 'B', 'C', 'D'],我想发送到谷歌Map搜索这些值一个接一个。
我的代码:

from selenium import webdriver
import time
from time import sleep
path = r"C:/Users/admin/chromium-browser/chromedriver.exe"
options = webdriver.ChromeOptions()
options.binary_location = r"C:\Users\sonpn.vbi\AppData\Local\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(path, chrome_options=options)
driver.get("https://www.google.co.in/maps/@10.8091781,78.2885026,7z")
sleep(2)
for index, item in enumerate(list_x, start=0):  
    Place = driver.find_element("class name", "tactile-searchbox-input").send_keys(item)
    Submit = driver.find_element("xpath", "//*[@id='searchbox-searchbutton']").click()
    result = driver.find_elements("xpath", '//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[2]/div[1]/div[1]')
    convert1 = [el.text for el in result]
    convert.extend(convert1)
    time.sleep(3)
    close = driver.find_element('xpath', '//*[@id="sb_cb50"]').click()

错误是
Web驱动程序异常:消息:无法访问chrome(会话信息: chrome = 97.0.4692.71)
请帮助我解决此错误。谢谢

iugsix8n

iugsix8n1#

此错误消息...

WebDriverException: Message: chrome not reachable (Session info: chrome=97.0.4692.71)

...表示ChromeDriver无法启动/产生新的 * 浏览上下文 *,即google-chrome会话。
您的主要问题是所使用的二进制文件版本之间的不兼容性,如下所示:

  • 可能您使用的是最新的 * chrome = 104.0 *
  • 但您使用的是 * chromedriver = 97.0 *

因此,* chromedriver = 97.0 * 和 * chrome = 104.0 * 之间存在明显的不匹配
溶液
确保:

66bbxpm5

66bbxpm52#

作为一种替代解决方案,您可以使用API,例如SerpApi的Google Maps Place Results API(付费API,具有在其后端处理块和解析的免费计划)。
开始,我们创建一个所需地址的列表和一个带有坐标的列表(坐标可以在所需地址的URL中找到):

# addresses from which we want to extract the name of the cities
addresses = [
    'Bälliz 22, Switzerland',
    'Blümlisalpstrasse 36, Switzerland',
    'Stauffacherstrasse 105, Switzerland',
    'Am Wasser 3, Switzerland',
    'Ringstrasse 18, Switzerland'
]

# GPS coordinates of location where you want your q (query) to be applied
# those coordinates are taken from the Google Maps URL
geo_coordinates = [
    '@46.7600484,7.6155472,14.08z',
    '@46.7810488,7.574276,14z',
    '@47.3792421,8.5218228,16z',
    '@47.4039247,8.5970111,16z',
    '@47.4139972,9.190949,13z'
]

接下来,我们使用zip()遍历列表,从API响应中获取完整地址,并使用regular expression提取城市名称:

city_name = re.search(r'\d+\s(\w+),', results['place_results']['address']).group(1)

检查在线IDE中的完整代码。

from serpapi import GoogleSearch
import json, re, os

# addresses from which we want to extract the name of the cities
addresses = [
    'Bälliz 22, Switzerland',
    'Blümlisalpstrasse 36, Switzerland',
    'Stauffacherstrasse 105, Switzerland',
    'Am Wasser 3, Switzerland',
    'Ringstrasse 18, Switzerland'
]

# GPS coordinates of location where you want your q (query) to be applied
geo_coordinates = [
    '@46.7600484,7.6155472,14.08z',
    '@46.7810488,7.574276,14z',
    '@47.3792421,8.5218228,16z',
    '@47.4039247,8.5970111,16z',
    '@47.4139972,9.190949,13z'
]

for address, coordinates in zip(addresses, geo_coordinates):
    params = {
      "api_key": "...",               # serpapi key, https://serpapi.com/manage-api-key
      "engine": "google_maps",        # SerpApi search engine
      "type": "search",               # list of results for the query
      "google_domain": "google.com",  # google domain
      "q": address,                   # query
      "hl": "en",                     # language
      "ll": coordinates               # GPS coordinates
    }
    
    search = GoogleSearch(params)     # where data extraction happens on the backend
    results = search.get_dict()       # JSON -> Python dict
    
    city_name = re.search(r'\d+\s(\w+),', results['place_results']['address']).group(1)
    print(f'City name: {city_name}')

输出示例:

City name: Thun
City name: Heimberg
City name: Zürich
City name: Dübendorf
City name: Neuenhof

如果你需要更多的代码解释和如何提取其他数据,有一篇Using Google Maps Place Results API from SerpApi using Python的博客文章。
免责声明我为SerpApi工作。

相关问题