3 回答

TA贡献1895条经验 获得超3个赞
编辑:
你得到了重复的结果,因为从循环它选择根元素//它应该是相对的或选择子元素,./但它仍然不起作用,并且可能是分裂错误。但尝试使用 CSS 选择器
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.....

TA贡献1864条经验 获得超2个赞
这是正确的,因为您不能在 for 循环中声明一个变量,然后在其中创建该变量。您需要在初始化循环之前创建变量才能使其工作。
title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")
for title_element in title_elements:
title = title_element.text
print(title)

TA贡献1982条经验 获得超2个赞
更改您的代码:
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)
添加回答
举报