如何使用Python / BeautifulSoup从Yahoo Finance中提取特定字段

问题描述 投票:1回答:1

我想提取公司的股票流通量。请以以下链接为例:查看源:https://finance.yahoo.com/quote/GE/key-statistics

我可以看到floatShares中的原始数据是我想要得到的:

"floatShares":{"raw":8733446536,"fmt":"8.73B","longFmt":"8,733,446,536"}

但是,当我使用BeautifulSoup时,即使我可以使用ctrl-F找到该信息,也无法找到该信息。谁能建议我如何使用Python / BeautifulSoup在名为8733446536的变量中捕获数字数据floatShares

谢谢

python beautifulsoup yahoo-finance
1个回答
2
投票

您可以使用re / json模块提取数据。

例如:

import re
import json
import requests


url = 'https://finance.yahoo.com/quote/GE/key-statistics'

html_text = requests.get(url).text
data = json.loads(re.search(r'root\.App\.main = (.*?\});\n', html_text).group(1))

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

print(data['context']['dispatcher']['stores']['QuoteSummaryStore']['defaultKeyStatistics']['floatShares'])

打印:

{'raw': 8733446536, 'fmt': '8.73B', 'longFmt': '8,733,446,536'}
© www.soinside.com 2019 - 2024. All rights reserved.