我用于抓取谷歌地图上餐厅信息的网络爬虫程序只返回一个空的字符串数组。应该怎么解决这个问题?

前端开发 2026-07-08

我正在做一个项目,我的程序从谷歌地图获取餐厅的详细信息(名称、位置、价格、评分等),然后将它们存入Python的 SQL数据库。我在测试代码,想看看它是否能只获取餐厅名称,然后把它存入 record array

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from bs4 import BeautifulSoup

import mysql.connector
import time
import requests


service = Service(executable_path="chromedriver.exe")
driver = webdriver.Chrome(service=service)

wait = WebDriverWait(driver,10)
count = 0
e_store = []
record = []



class Restaurants():
    #stores the values from the google maps
    def __init__(self):
        self.name = None
        self.address = None
        self.rating = None
        self.website = None

        self.phone = None


    def open_browser(self):
        #To open the chrome web browser 

        driver.maximize_window()
        driver.get('https://www.google.com/maps')

        try:
            but =  wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Accept all']"))) # make sure to make your input into a single tuple 
            but.click()




        except Exception as e:
            print("Element not found", e)

    def search_term(self):
        add_term = driver.find_element(By.XPATH, "//input[@name='q']")
        ActionChains(driver)\
            .send_keys_to_element(add_term, "Restaurants in my area")\
            .key_down(Keys.RETURN)\
            .perform()


        time.sleep(5)

    def get_all(self):
        global count
        #looking for restaurant profile
        terms = driver.find_elements(By.XPATH, "//a[@class='hfpxzc']")
        #This allows selenium to simulate user actions
        action =  ActionChains(driver)

        # I think this is the code for scrolling
        while len(terms) < 1000:

            length = len(terms)
            #scroll away from staring profile
            scroll_origin = ScrollOrigin.from_element(terms[length-1])
            action.scroll_from_origin(scroll_origin, 0, 1000).perform()
            #wait for 2 seconds
            time.sleep(2)
            terms = driver.find_elements(By.XPATH, "//a[@class='hfpxzc']")

            #Check if any new restraunt profile has appeared
            if len(terms) == length:
                count += 1
                if count > 20:
                    break
            else:
                count = 0

        for i in range(len(terms)):

            scroll_origin =  ScrollOrigin.from_element(terms[i])
            action.scroll_from_origin(scroll_origin, 0,100).perform()
            action.move_to_element(terms[i]).perform()
            terms[i].click()
            time.sleep(2)
            source = driver.page_source
            soup = BeautifulSoup(source, 'html.parser')

            try:
                div_find = soup.find_all('div', class_="XltNde tTVLSc")

                for divs in div_find:
                    name = divs.find('span', class_="a5H0ec")

                    if name:
                        record.append(name.text.strip())
                        print(record)

            except Exception as e:
                print("Failed:", e)
                continue


# main program
if __name__ == "__main__":

    get_details = Restaurants()

    get_details.open_browser()

    get_details.search_term()

    get_details.get_all()

当我运行程序时,得到的结果是:

Cookie popup accepted

['']

['', '']

['', '', '']

['', '', '', '']

['', '', '', '', '']

['', '', '', '', '', '']

我认为问题可能出在span标签上,我正在尝试从中获取餐厅名称。

对于这个问题的任何帮助都将非常有用

name = divs.find('span', class_="a5H0ec")

解决方案

问题在于名称并非位于 <span> 的内部,而是在 <span> 之后(且span的文本为空)

<h1 class="DUwDvf lfPIob">
   <span class="a5H0ec"></span>  <!-- empty span -->
   Restaurant Name
   <span class="G0bp3e"></span>  <!-- another empty span -->
</h1>

你可能需要使用 .next_sibling(在较旧的版本中是 .nextSibling

name = divs.find("span", class_="a5H0ec").next_sibling

最终你应该搜索 <h1>

name = divs.find("h1", class_="DUwDvf lfPIob")

顺便说一句:

如果你得到 <h1>,那么你可以使用

print(name.encode_contents())

来查看内部HTML并清空 <span>

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章