【问题标题】:scraping price from the website using python使用 python 从网站上抓取价格
【发布时间】:2022-01-08 07:14:39
【问题描述】:

我尝试从以下网站抓取价格: https://www.emma-sleep.com.au/diamond-hybrid/ 我想要的价格应该是674.55

下面是我的代码:

web_url = 'https://www.emma-sleep.com.au/diamond-hybrid/'
web_response = requests.get(web_url)
b_soup = BeautifulSoup(web_response.text, 'html.parser')
price=b_soup.find_all('span', {'class': 'installment__full-price'})
price

它会以某种方式返回我 1000 的价格,这是错误的 [$1,000.00]

谁能告诉我我哪里弄错了? 当我检查 html 代码时,价格似乎在“脚本”标签中。

谢谢:)

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    价格以javascript计算。为此,您需要使用像 Selenium 这样的 API,它首先在浏览器中呈现页面,然后允许您访问 HTML。

    目标html标签:

    <span class="regular-price text-orange" id="product-price-3853">
       <span class="price" data-price="674.55">$674.55</span>
    </span>
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    web_url = 'https://www.emma-sleep.com.au/diamond-hybrid/'
    
    s = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=s)
    
    # web driver goes to page
    driver.get(web_url)
    
    # to give time for the page to load
    driver.implicitly_wait(3)
    
    price = driver.find_element(By.XPATH, "//span[@class='regular-price text-orange']/span[@class='price']").text
    print(price)
    

    输出:

    $674.55
    

    您也可以使用 XPATH 表达式:

    //span[contains(@class, 'regular-price')]/span[@class='price']
    

    【讨论】:

    • 非常感谢您完美运行 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多