Sam Sagariya
Sam Sagariya

Reputation: 33

Beautiful soup extract text between span tags

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

I need to extract the numbers 33,990.00 from the above html.

Upvotes: 0

Views: 1436

Answers (3)

teller.py3
teller.py3

Reputation: 844

Why use selenium? It's so unnecessary. Only use selenium if the page is JavaScript rendered. Otherwise use the following:

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()

Output:

33,990.00

Upvotes: 0

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15568

With 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())

Upvotes: 1

Lukas
Lukas

Reputation: 446

This is a good job for 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

Upvotes: 0

Related Questions