Arthur Law
Arthur Law

Reputation: 131

Beautiful soup extract data from HTML sources code?

I tried to extract "Reported EPS Basic" from web page - "https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK" . The data 0.23, 0.2 appears in the follow format after running my codes, How to extract these number form the follow source code?

"div class="D(tbc) Ta(end) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg" data-test="fin-col" data-reactid="292">0.23

div class="D(tbc) Ta(end) Pstart(6px) Pend(4px) Bxz(bb) Py(8px) BdB Bdc($seperatorColor) Miw(100px) Miw(156px)--pnclg Bgc($lv1BgColor) fi-row:h_Bgc($hoverBgColor)" data-test="fin-col" data-reactid="293">0.20

My codes:

url="https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK"
result = requests.get(url)
result.raise_for_status()
result.encoding = "utf-8"


src = result.content
soup = BeautifulSoup(src, 'lxml')
#soup = BeautifulSoup(src, 'html5lib')
#print(soup.prettify())
print(soup)

with open('soup.txt','w') as f:
    f.write(str(src))

Upvotes: 0

Views: 80

Answers (1)

Vinod Sawant
Vinod Sawant

Reputation: 623

Try this,

import requests
import bs4

url = 'https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK'
data = requests.get(url)

soup = bs4.BeautifulSoup(data.text,'html.parser')

soup.find_all('div',attrs={"data-reactid":"292"})[0].text
soup.find_all('div',attrs={"data-reactid":"293"})[0].text

Upvotes: 3

Related Questions