简体   繁体   English

美汤从 HTML 源代码中提取数据?

[英]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 " .我试图从网页 - “ https://finance.yahoo.com/quote/1928.HK/financials?p=1928.HK ”中提取“Reported EPS Basic”。 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?运行我的代码后,数据0.23、0.2以如下格式出现,如何从以下源代码中提取这些数字?

"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" 数据测试="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 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))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM