Reputation: 51
I'm trying to scrape the dividend yield on this website (https://www.set.or.th/set/companyprofile.do?symbol=FTREIT&ssoPageId=4&language=en&country=US)
But it's wrapped in "col-xs-9 col-md-5" and this appears multiple times.
There's a piece of text "Dvd. Yield(%)" that only appears once.
I know how to search for "Dvd. Yield(%)" but don't know how to get to the next line which has the numeric dividend yield number.
Beginner in python so appreciate the advice! Thanks a million in advance!
import requests
from bs4 import BeautifulSoup
URL = 'https://www.set.or.th/set/companyprofile.do?symbol=FTREIT&ssoPageId=4&language=en&country=US'
page = requests.get(URL)
print(page)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find_all('Dvd. Yield(%)')
Upvotes: 0
Views: 274
Reputation: 125
You can first find the parent element and then go to the second child DIV element to obtain the required one. Here is the sample code:
result = soup.find('div', string='Dvd. Yield(%)') # finding the div element by text
parent = result.parent # finding the parent
dvd_yield = parent.find_next('div').find_next('div') # getting the second div child element
print(dvd_yield.text) # printing the result
Result:
3.83
Upvotes: 1