0

我正在尝试在该网站上获取股息收益率(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(%)')

4

1 回答 1

1

可以先找到父元素,然后再去第二个子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
于 2020-03-04T10:37:23.620 回答