Home > Blockchain >  Google map error list indices must be integers or slices, not WebElement
Google map error list indices must be integers or slices, not WebElement

Time:06-06

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 enter image description here

CodePudding user response:

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, '//*[@]')]

        # 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()

Output -

 92 334 5555859
 92 42 35749000
 92 322 3368251
 92 333 4254716
....
....
  • Related