地图餐厅列表抓取

Maps Restaurant List Scrape

我正在尝试使用 python selenium 从 googlemaps 中抓取城市中的餐厅列表,但是,我无法使用我尝试过的代码提取元素(餐厅名称)。

使用 inspect 元素,我点击餐厅标题,这是元素详细信息:

h3 class="section-result-title"> 

span jstcache="126">Amrutha Lounge</span> 

button jstcache="127" style="display:none"></button> </h3>

我想提取餐厅名称,在上面的元素中是'Amrutha Lounge'

第二个餐厅元素(明亮):

h3 class="section-result-title"> 


span jstcache="126">Bright</span> 


button jstcache="127" style="display:none"></button> </h3>

我尝试通过下面编写的代码提取名称,但是,到目前为止没有成功。

from selenium import webdriver

driver= 
webdriver.Chrome(executable_path="C:/users/usr/Desktop/chromedriver.exe")

UrlA = "https://www.google.com/maps/search/"
UrlB= "London"
UrlC="Restaurants"
UrlD= UrlA + UrlB + '+' + UrlC
driver.get(UrlD)

x=driver.find_elements_by_class_name('section-result-title')
print(x)

这是我目前得到的输出:

selenium.webdriver.remote.webelement.WebElement (session="db301e2fc353297f0e9799b9a5fefd2f", element="c39f1e01-0795-4423-8b1e-61ee5b4e5048")>, <selenium.webdriver.remote.webelement.WebElement (session="db301e2fc353297f0e9799b9a5fefd2f", element="39e7a7e7-2dae-4638-9664-391f3a00dcb7"

这是我想要得到的输出:

Amrutha Lounge

Bright

Bagatelle London

...

您正在打印 WebElement。您需要打印 String.

因为您已经将 WebElements 列表存储在变量 x 中。您可以简单地遍历它们并打印 WebElement.text 以获取文本

https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.text

所以只需将您的代码更改为

for elm in x:
    print(elm.text)