我收到以下错误:
无效数字'来自经营活动的总现金流量'过去了。
来自这个网址:
Scrape Yahoo Finance Income Statement with Python
"这有点困难,因为"净收入"用一个强大的标签括起来"
有人可以向我解释为什么这段代码无法用于运营活动的总现金流量以及我如何确定某些东西有强烈的标签?
代码:
import re, requests
from bs4 import BeautifulSoup
import sys
"""
import os # file system operations
import re # regular expressions
import pandas as pd # pandas... the best time series library out there
import datetime as dt # date and time functions
import io
"""
# search with regular expressions
# "CrumbStore":\{"crumb":"(?<crumb>[^"]+)"\}
def get_crumb():
url = 'https://uk.finance.yahoo.com/quote/AAPL/history' # url for a ticker symbol, with a download link
r = requests.get(url) # download page
txt = r.text # extract html
cookie = r.cookies['B'] # the cooke we're looking for is named 'B'
print('Cookie: ', cookie)
# Now we need to extract the token from html.
# the string we need looks like this: "CrumbStore":{"crumb":"lQHxbbYOBCq"}
# regular expressions will do the trick!
pattern = re.compile('.*"CrumbStore":\{"crumb":"(?P<crumb>[^"]+)"\}')
for line in txt.splitlines():
m = pattern.match(line)
if m is not None:
crumb = m.groupdict()['crumb']
print('Crumb=',crumb)
return crumb
def periodic_figure_values(soup, yahoo_figure):
values = []
pattern = re.compile(yahoo_figure)
title = soup.find("strong", text=pattern) # works for the figures printed in bold
if title:
row = title.parent.parent
else:
title = soup.find("td", text=pattern) # works for any other available figure
if title:
row = title.parent
else:
sys.exit("Invalid figure '" + yahoo_figure + "' passed.")
cells = row.find_all("td")[1:] # exclude the <td> with figure name
for cell in cells:
if cell.text.strip() != yahoo_figure: # needed because some figures are indented
str_value = cell.text.strip().replace(",", "").replace("(", "-").replace(")", "")
if str_value == "-":
str_value = 0
value = int(str_value) * 1000
values.append(value)
return values
def financials_soup(ticker_symbol, statement, quarterly=False):
if statement == "is" or statement == "bs" or statement == "cf":
crumb = get_crumb()
url = "https://finance.yahoo.com/q/" + statement + "?s=" + ticker_symbol + "&crumb=" + crumb
if not quarterly:
url += "&annual"
return BeautifulSoup(requests.get(url).text, "html.parser")
return sys.exit("Invalid financial statement code '" + statement + "' passed.")
print(periodic_figure_values(financials_soup("AAPL", "cf"), "Total Cash Flow From Operating Activities"))
编辑:
我可以通过将financials_soup函数更改为以下内容来获取结果:
def financials_soup(ticker_symbol, statement, quarterly=False):
if statement == "financials" or statement == "balance-sheet" or statement == "cash-flow":
crumb = get_crumb()
url = "https://finance.yahoo.com/quote/" + ticker_symbol + "/" + statement + "?p=" + ticker_symbol + "&crumb=" + crumb
if not quarterly:
url += "&annual"
return BeautifulSoup(requests.get(url).text, "html.parser")
return sys.exit("Invalid financial statement code '" + statement + "' passed.")
答案 0 :(得分:0)
如果您检查链接返回的来源(第69行):
url =&#34; https://finance.yahoo.com/q/&#34; +声明+&#34;?s =&#34; + ticker_symbol +&#34;&amp; crumb =&#34; + crumb
您会发现它没有关于Total Cash Flow From Operating Activities
的任何有用信息。如果你把它改成
url =&#34; https://finance.yahoo.com/quote/&#34; + ticker_symbol +&#34; / cash-flow?p =&#34; + ticker_symbol
它会返回您正在寻找的信息:
[65824000000,81266000000,59713000000]