gpt4 book ai didi

python - 谷歌地图错误列表索引必须是整数或切片,而不是 WebElement

转载 作者:行者123 更新时间:2023-12-05 03:23:29 27 4
gpt4 key购买 nike

我正在尝试抓取电话号码,但他们向我显示您的list must be integer not a web element的错误我已经尝试了不同的方法,但他们会显示我这些错误这是链接 https://www.google.com/maps/search/dentist+uk/@31.4820415,74.2444157,12z/data=!3m1!4b1

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
options = webdriver.ChromeOptions()

# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")

chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)

data=[]
def supplyvan_scraper():
with chrome_driver as driver:
driver.implicitly_wait(15)
URL = 'https://www.google.com/maps/search/dentist+uk/@31.5688259,74.2388013,12z/data=!3m1!4b1'
driver.get(URL)
time.sleep(3)
page_links = [element.get_attribute('href') for element in driver.find_elements(By.XPATH, '//*[@class="hfpxzc"]')]

# visit all the links
for link in page_links:
wev={}
driver.get(link)
time.sleep(2)
link=driver.find_elements(By.XPATH, "//div[@class='RcCsl fVHpi w4vB1d NOE9ve M0S7ae AG25L']")
for i in link:
if '+' in link[i].get():
phone=driver.find_element(By.XPATH, "//div[@class='Io6YTe fontBodyMedium']").text
print(phone)


time.sleep(2)
supplyvan_scraper()

在这些行中显示错误:

if '+' in link[i]:

enter image description here

最佳答案

您可以使用正则表达式来检查电话号码并轻松打印出来

电话号码仅包含 数字 并以 + 符号开头所以你可以使用这个模式 - ^\+.*\d$

您可以搜索所有文本元素并使用此模式查找数字

工作代码-

def google_map_scraper():
with chrome_driver as driver:
driver.implicitly_wait(15)
URL = 'https://www.google.com/maps/search/dentist+uk/@31.5688259,74.2388013,12z/data=!3m1!4b1'
driver.get(URL)
time.sleep(3)
page_links = [element.get_attribute('href') for element in
driver.find_elements(By.XPATH, '//*[@class="hfpxzc"]')]

# visit all the links
for link in page_links:
driver.get(link)
time.sleep(2)
link = driver.find_elements(By.XPATH, "//div[@class='RcCsl fVHpi w4vB1d NOE9ve M0S7ae AG25L']")
for i in link:
if re.search('^\+.*\d$', i.text): # find the phone number starting with + and containing only digits
phone_num = i.text
print(phone_num) # print the phone number

time.sleep(2)


google_map_scraper()

输出-

+92 334 5555859
+92 42 35749000
+92 322 3368251
+92 333 4254716
....
....

关于python - 谷歌地图错误列表索引必须是整数或切片,而不是 WebElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72507145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com