Google Map错误列表索引必须是整数或切片,而不是webelement

发布于 2025-02-05 00:54:28 字数 1965 浏览 1 评论 0 原文

我正在尝试刮擦电话号码,但它们向我展示了您的列表必须是整数而不是Web元素我尝试了不同的批准,但他们会向我展示这些错误这是链接

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]:

I am trying to scrape the phone number but they show me the error that your list must be integer not a web element I Have try different approches but they will show me these error this is the link 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()

Show error in these line:

if '+' in link[i]:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2025-02-12 00:54:28

您可以使用Regex检查电话号码并轻松打印出

电话号码仅包含 DIGITS ,并以+标志开始
因此,您可以使用此模式 - ^\+。*\ 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

输出 -输出 -

+92 334 5555859
+92 42 35749000
+92 322 3368251
+92 333 4254716
....
....
, 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()

输出 -输出 -


You can use regex to check the phone number and print it out easily

Phone number contains only digits and starting with a + sign
So you may use this pattern - ^\+.*\d$

You can search all the text elements and find the number using this pattern

working code -

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

Output -

+92 334 5555859
+92 42 35749000
+92 322 3368251
+92 333 4254716
....
....
, 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()

Output -



    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文