如何在此html中获取价格信息?

时间:2020-06-14 15:20:04

标签: python xpath scrapy

我要在此页面中获得产品名称和价格,请遵循我的代码:

r = requests.get("https://www.paodeacucar.com/produto/67746/")
product_html = parser.fromstring(r.text)
name = product_html.xpath("//h2[@class='hidden-xs hidden-sm ng-binding']/text()") price_str =
product_html.xpath("//p[@class='normal-price ng-binding ng-scope']/text()")

Inpector

1 个答案:

答案 0 :(得分:0)

您将需要Selenium,因为wesbite使用javascript来显示价格。 要使用Selenium选择产品的价格,您将需要类似:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver.get('https://www.paodeacucar.com/produto/67746/')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//schema[@itemprop='price']"))).get_attribute("content"))