美丽的汤提取跨度标签之间的文本

时间:2018-10-12 16:14:00

标签: python beautifulsoup

<span id="priceblock_dealprice" class="a-size-medium a-color-price"><span class="currencyINR">&nbsp;&nbsp;</span> 33,990.00 </span>

我需要从上面的html中提取数字33,990.00。

3 个答案:

答案 0 :(得分:1)

搭配beautifulsoup:

from bs4 import BeautifulSoup as bs

content = '''<span id="priceblock_dealprice" class="a-size-medium a-color-price"><span class="currencyINR">&nbsp;&nbsp;</span> 33,990.00 </span>'''

soup = bs(content,'html5lib')
print(soup.text.strip())

答案 1 :(得分:0)

这对selenium来说是一项好工作:

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

browser = webdriver.Firefox()

browser.get(URL)

delay = 30  # seconds
WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'priceblock_dealprice')))
print("Page is ready!")

text = browser.find_element_by_id("priceblock_dealprice").text

答案 2 :(得分:0)

为什么使用selenium?没必要如果页面是JavaScript呈现的,则仅使用selenium。否则请使用以下内容:

from bs4 import BeautifulSoup
html = '<span id="priceblock_dealprice" class="a-size-medium a-color-price"><span class="currencyINR">&nbsp;&nbsp;</span> 33,990.00 </span>'
soup = BeautifulSoup(html, 'lxml')
text = soup.select_one('span.a-color-price').text.strip()

输出:

33,990.00