我正在尝试从Yahoo "Analysis“选项卡中提取”未来5年(每年)“股票BABA的价值:https://finance.yahoo.com/quote/BABA/analysis?p=BABA。(从底部到第二排是2.85% )。
我一直在试图利用这些问题:
Scrape Yahoo Finance Financial Ratios
Scrape Yahoo Finance Income Statement with Python
但我甚至不能从页面中提取数据
也尝试过这个网站:
https://hackernoon.com/scraping-yahoo-finance-data-using-python-ayu3zyl
这是i代码编写的获取网页数据。
首先进口包装:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq然后尝试从页面中提取数据:
Url= "https://finance.yahoo.com/quote/BABA/analysis?p=BABA"
r = requests.get(Url)
data = r.text
soup = BeautifulSoup(data,features="lxml")当查看“数据”和“汤”对象的类型时,我看到了
type(data)
<class 'str'>我可以使用正则表达式以某种方式提取“>未来5年”行所需的数据。
但当你看到
type(soup)
<class 'bs4.BeautifulSoup'>由于某些原因,其中的数据与页面无关。
看起来是这样的(只复制了小中汤对象中的部分内容):
soup
<!DOCTYPE html>
<html class="NoJs featurephone" id="atomic" lang="en-US"><head prefix="og:
http://ogp.me/ns#"><script>window.performance && window.performance.mark &&
window.performance.mark('PageStart');</script><meta charset="utf-8"/>
<title>Alibaba Group Holding Limited (BABA) Analyst Ratings, Estimates &
Forecasts - Yahoo Finance</title><meta con
tent="recommendation,analyst,analyst
rating,strong buy,strong
sell,hold,buy,sell,overweight,underweight,upgrade,downgrade,price target,EPS
estimate,revenue estimate,growth estimate,p/e
estimate,recommendation,analyst,analyst rating,strong buy,strong
sell,hold,buy,sell,overweight,underweight,upgrade,downgrade,price target,EPS
estimate,revenue estimate,growth estimate,p/e estimate" name="keywords"/>
<meta content="on" http-equiv="x-dns-prefetch-control"/><meta content="on"
property="twitter:dnt"/><meta content="90376669494" property="fb:app_id"/>
<meta content="#400090" name="theme-color"/><meta content="width=device-
width, 提前谢谢
发布于 2020-06-08 16:26:31
一种解决方案是使用regex从JS中的JSON数据中提取值。JSON数据位于以下变量中:
root.App.main = { .... };例子:
import requests
import re
import json
r = requests.get("https://finance.yahoo.com/quote/BABA/analysis?p=BABA")
data = json.loads(re.search('root\.App\.main\s*=\s*(.*);', r.text).group(1))
field = [t for t in data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]["earningsTrend"]["trend"] if t["period"] == "+5y" ][0]
print(field)
print("Next 5 Years (per annum) : " + field["growth"]["fmt"])https://stackoverflow.com/questions/62265281
复制相似问题