user3476463
user3476463

Reputation: 4575

parse text from span class using beautiful soup

I'm using beautiful soup to try to scrape the morning star rating out of yahoo finance. I have the span class below getting returned. Can anyone suggest how to return the "★★★★★" as a string from the soup below?

url:

https://finance.yahoo.com/quote/RFKTX?p=RFKTX

input:

<td class="Ta(end) Fw(600) Lh(14px)" data-test="MORNING_STAR_RATING-value"><span class="D(ib)">★★★★★</span><span class="D(ib) C($c-fuji-grey-d)"></span></td>

output:

★★★★★

Upvotes: 0

Views: 250

Answers (1)

SIM
SIM

Reputation: 22440

This should do it:

import requests
from bs4 import BeautifulSoup

link = 'https://finance.yahoo.com/quote/RFKTX?p=RFKTX'

with requests.Session() as s:
    s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    res = s.get(link)
    soup = BeautifulSoup(res.text,"html.parser")
    rating = soup.select_one("td[data-test='MORNING_STAR_RATING-value'] > span").text
    print(rating)

Upvotes: 2

Related Questions