0

I want to extract the value of a span via a CSS selector

soup = BeautifulSoup("<body><main><div class='rentalprice'><span>3.000</span></div></main></body>")
pagecontent = soup.find('body')

price = pagecontent.main.div.span.text # this line works although it's not suitable for production since I've now ignored the div class selector

#I then tried this:
price = pagecontent.select('main div.rentalprice span').text

The last line throws error:

Exception has occurred: AttributeError. ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

How can I fix this? I don't want to use a for loop since there's only 1 span that matches.

I already checked: How to get text from span tag in BeautifulSoup Get value of attribute using CSS Selectors with BeutifulSoup Python Selenium CSS Selector by Span get_attribute

2 Answers 2

1

select returns a list of elements, but you can only use .text on a single element.

If you're sure that there's only 1 span that matches, you can target the first element in the list returned by select:

price = pagecontent.select('main div.rentalprice span')[0].text
print(price) # 3.000

Otherwise, you can use a for loop:

for elem in pagecontent.select('main div.rentalprice span'): 
    print(elem.text)
Sign up to request clarification or add additional context in comments.

Comments

0

use .find()

from bs4 import BeautifulSoup

soup = BeautifulSoup("<body><main><div class='rentalprice'><span>3.000</span></div></main></body>", 'html.parser')
pagecontent = soup.find('body')
price = pagecontent.find('span').text
print(price) # 3.000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.