【发布时间】:2020-06-08 15:13:24
【问题描述】:
我正在尝试从雅虎财经的“分析”选项卡中提取股票 BABA 的“未来 5 年(每年)”的价值: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
这是我写的获取网页数据的代码
首先导入包:
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'>
并且由于某种原因,其中的数据与页面无关。
看起来像那样(仅复制了汤对象中的 small 部分内容):
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,
- 有没有其他方法可以从对象数据中提取非正则表达式所需的数据?
- soup 对象如何帮助我提取数据(我看到它被大量使用,但不知道如何变得有用)?
提前致谢
【问题讨论】:
-
您可以使用soup 对象在您的页面上找到相应的元素。按照本教程中描述的步骤操作:link。我尝试了链接,但我的汤对象看起来不像您添加的链接(我对问题进行了一些编辑,以便您查看外观)。汤对象似乎没有来自页面的数据,而由于某种原因,数据对象确实包含来自页面的信息
标签: python web-scraping beautifulsoup