我想打印一些房价,并且在使用Xpath时遇到了麻烦。这里是我的代码:使xpath更具选择性? [网络刮]
from selenium import webdriver
driver = webdriver.Chrome("my/path/here")
driver.get("https://www.realtor.com/realestateandhomes-search/?pgsz=10")
for house_number in range(1,11):
try:
price = driver.find_element_by_xpath("""//*[@id="
{}"]/div[2]/div[1]""".format(house_number))
print(price.text)
except:
print('couldnt find')
我在this网站,试图打印关闭前十楼的房子的房价。
我的输出是对于所有说“新”的房屋,这个价格取代实际价格。但是对于没有新贴纸的最下面两个,记录了实际价格。
如何制作我的Xpath选择器,以便它选择数字而不是NEW?
嗨,感谢您的努力,我欣赏评论和深思熟虑的解释。但是,当我尝试运行该代码时,我现在发现一个错误,即Selenium根本找不到该元素(即,对于任何房屋)! 我将我的代码更改为: 'price = driver.get_element_by_xclass(“”“// li [@id =”{}“]/div [@ class =”srp-item-body“]/div [@class =“srp-item-price”]“”“。format(house_number))'\t 而这会抛出一个异常,即每次都无法找到元素。 – thewhitetie
它在我的Chrome控制台中工作,你尝试使用'driver.find_element_by_xpath'吗? –