不确定我是否在这里犯了一个愚蠢的错误,我已经搜索了所有内容,但我无法弄清楚这一点。我真的很感激你的帮助。
我正在尝试制作一个抓取工具来抓取 Google Map Pack 数据。我正在使用 Splinter 来做到这一点。我已经设法选择了每个地图包项目的 div,但我想然后遍历并选择每个 div 的标题(和其他元素)。
但是,当我尝试这样做时,它总是选择第一个元素的标题,即使我在单个元素上运行 find_by_xpath 也是如此。
这是我的代码:
from splinter import Browser
from selenium import webdriver
import time
chrome_options = webdriver.ChromeOptions()
browser = Browser('chrome', options=chrome_options)
browser.visit("https://google.com")
browser.fill('q', 'roofing laredo tx')
# Find and click the 'search' button
time.sleep(5)
button = browser.find_by_name('btnK')
# Interact with elements
button.click()
time.sleep(5)
maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")
for map_element in maps_elements:
# print(map_element.text)
title = map_element.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
print(title)
所以我想要的是:JJ Flores Roofing & Construction HBC Roofing McAllen Valley Roofing Co
但我得到了
JJ弗洛雷斯屋面和建筑 JJ弗洛雷斯屋面和建筑 JJ弗洛雷斯屋面和建筑
蛊毒传说
慕斯王
临摹微笑
相关分类