How to print item in a list without a for loop

Viewed 40

I'm trying to just get the price off a website and found that "class="udYkAW2UrhZln2Iv62EYb" " gave me the price in one line. but when I try to print it out I keep getting

<span class="udYkAW2UrhZln2Iv62EYb">$0.312423</span>

and not just the price itself. I fixed this by using a for loop to get me item, but is there a way to just display the price with a print function without a for loop? Please and thank you.

Here's the code

from bs4 import BeautifulSoup as bs
import requests

url = 'https://robinhood.com/crypto/DOGE'
r = requests.get(url)

#make to soup 
soup = bs(r.content, 'lxml')

#where the price of the search was found "span class='udYkAW2UrhZln2Iv62EYb'"
#Using find() because this is the first instance of this class
price_class = soup.find('span', {'class' : 'udYkAW2UrhZln2Iv62EYb'})
print(price_class)
type(price_class)
    #outout: <span class="udYkAW2UrhZln2Iv62EYb">$0.312423</span>
    #output: bs4.element.Tag

for i in price_class:
    print(i)
        #output: $0.312423
1 Answers

Use .text or .get_text():

from bs4 import BeautifulSoup as bs
import requests

url = "https://robinhood.com/crypto/DOGE"
r = requests.get(url)

soup = bs(r.content, "lxml")

price = soup.find("span", {"class": "udYkAW2UrhZln2Iv62EYb"})
print(price.text)   # <--- use .text

Prints:

$0.315917
Related