我正在尝试在此网站上获取股息收益率(https://www.set.or.th/set/companyprofile.do?symbol=FTREIT&ssoPageId=4&language=en&country=US)
但是它包裹在“ col-xs-9 col-md-5”中,并且出现多次。
有一段文字“ Dvd。Yield(%)”仅出现一次。
我知道如何搜索“ Dvd。Yield(%)”,但不知道如何到达具有数字股息收益率数字的下一行。
Python入门者,不胜感激建议!预先感谢一百万!
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(%)')
您可以先找到父元素,然后转到第二个子DIV元素以获得所需的元素。这是示例代码:
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
结果:
3.83