如何解析商品的价格?
from selenium import webdriver
from selenium.webdriver.common.by import By
url='https://steamcommunity.com/market/listings/730/Recoil%20Case'
options=webdriver.ChromeOptions()
driver=webdriver.Chrome(executable_path='C:\\Users\\user\\Downloads\\chromedriver_win32/chromedriver.exe',options=options)
try:
driver.get(url=url)
normal_price=driver.find_element(By.XPATH,'//*[@id="market_commodity_forsale"]/span[2]').text
print(normal_price)
except Exception as ex:
print(ex)
finally:
driver.close()
driver.quit()
显然 selenium 试图在页面加载之前找到元素。尝试等待元素出现。
https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait