Xpath 未使用 Splinter/Selenium Python 选择正确的元素 3
Xpath Not Selecting Correct Element Using Splinter/Selenium Python 3
不确定我是否在这里犯了一个愚蠢的错误,我已经搜索了所有但我无法弄清楚这一点。非常感谢您的帮助。
我正在尝试制作一个抓取器来抓取 Google 地图包数据。我正在使用 Splinter 这样做。我已经设法 select 每个地图包项目的 div 但我想然后遍历 select 每个 div 的标题(和其他元素) s.
但是,当我尝试这样做时,它总是 select 第一个元素的标题,即使我是 运行 indiv 上的 find_by_xpath idual 元素.
这是我的代码:
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)
所以我想要的是:
J J Flores 屋面施工
HBC屋面
麦卡伦谷屋顶公司
但我得到
J J Flores 屋顶与建筑
J J Flores 屋面施工
J J Flores 屋面施工
编辑:
你得到了重复的结果,因为从循环它 selecting 根元素 //
它应该是相对的或 ./
到 select 孩子但它仍然不起作用并且也许分裂错误。但尝试使用 CSS select 或
for map_element in maps_elements:
# select relative but failed
#title = map_element.find_by_xpath("./div[contains(@class,'dbg0pd')]/span")
title = map_element.find_by_css("div[class*='dbg0pd'] > span").text
print(title)
变量输入错误,从
中删除 s
title = maps_elements.....
#title = map_element.....
更改您的代码:
maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")
for map_element in maps_elements:
# print(map_element.text)
title = maps_elements.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
print(title)
至
title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")
for title_element in title_elements:
title = title_element.text
print(title)
这是正确的,因为您不能在 for 循环中声明一个变量,然后在其中创建该变量。您需要在初始化循环之前创建变量才能使其工作。
title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")
for title_element in title_elements:
title = title_element.text
print(title)
不确定我是否在这里犯了一个愚蠢的错误,我已经搜索了所有但我无法弄清楚这一点。非常感谢您的帮助。
我正在尝试制作一个抓取器来抓取 Google 地图包数据。我正在使用 Splinter 这样做。我已经设法 select 每个地图包项目的 div 但我想然后遍历 select 每个 div 的标题(和其他元素) s.
但是,当我尝试这样做时,它总是 select 第一个元素的标题,即使我是 运行 indiv 上的 find_by_xpath idual 元素.
这是我的代码:
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)
所以我想要的是: J J Flores 屋面施工 HBC屋面 麦卡伦谷屋顶公司
但我得到
J J Flores 屋顶与建筑 J J Flores 屋面施工 J J Flores 屋面施工
编辑:
你得到了重复的结果,因为从循环它 selecting 根元素 //
它应该是相对的或 ./
到 select 孩子但它仍然不起作用并且也许分裂错误。但尝试使用 CSS select 或
for map_element in maps_elements:
# select relative but failed
#title = map_element.find_by_xpath("./div[contains(@class,'dbg0pd')]/span")
title = map_element.find_by_css("div[class*='dbg0pd'] > span").text
print(title)
变量输入错误,从
中删除s
title = maps_elements.....
#title = map_element.....
更改您的代码:
maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")
for map_element in maps_elements:
# print(map_element.text)
title = maps_elements.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text
print(title)
至
title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")
for title_element in title_elements:
title = title_element.text
print(title)
这是正确的,因为您不能在 for 循环中声明一个变量,然后在其中创建该变量。您需要在初始化循环之前创建变量才能使其工作。
title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")
for title_element in title_elements:
title = title_element.text
print(title)