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

Adam
  • 6,041
  • 36
  • 120
  • 208

2 Answers2

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)
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
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
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21