簡體   English   中英

如何從雅虎財經中提取表格?

[英]How to extract a table from Yahoo finance?

我正在嘗試從 yahoo Finance 中提取財務表。 我正在使用 thimport pandas 作為 pd

import requests
from bs4 import BeautifulSoup
url="https://finance.yahoo.com/quote/FB/financials?p=FB"
headers={"User-Agent":"Mozilla/5.0"}
r=requests.get(url,headers=headers)
soup=BeautifulSoup(r.content, "html.parser")
stattable=soup.findAll('div', class_="M(0) Whs(n) BdEnd Bdc($seperatorColor) D(itb)")
stattable=stattable[0]
breakdown=[]

for row in stattable.findAll("div"):
  for cell in row.findAll(class_="D(ib) Va(m) Ell Mt(-3px) W(215px)--mv2 W(200px) undefined"):
    breakdown.append(cell.text)

正在提取的數據不正確並且正在自我復制。 以下是一小部分數據:

'Breakdown', 'ttm', '12/31/2019', '12/31/2018', '12/31/2017', '12/31/20
16', 'Breakdown', 'ttm', '12/31/2019', '12/31/2018', '12/31/2017', '12/3
1/2016', 'Breakdown', 'ttm', '12/31/2019', '12/31/2018', '12/31/2017', '
12/31/2016', 'Total Revenue75,157,00070,697,00055,838,00040,653,000-', '
', '75,157,000', '70,697,000', '55,838,000', '40,653,000', '', 'Cost of 
Revenue13,935,00012,770,0009,355,0005,454,000-', '13,935,000', '12,770,0
00', '9,355,000', '5,454,000', '', 'Gross Profit61,222,00057,927,00046,4
83,00035,199,000-', '61,222,000', '57,927,000', '46,483,000', '35,199,00
0', '', 'Operating Expense33,323,00033,941,00021,570,00014,996,000-', ''
, '33,323,000', '33,941,000', '21,570,000', '14,996,000', '', 'Operating
 Income27,899,00023,986,00024,913,00020,203,000-', '27,899,000', '23,986
,000', '24,913,000', '20,203,000', '', 'Net Non Operating Interest Incom
e Expense877,000904,000652,000392,000-', '', '877,000', '904,000', '652,
000', '392,000', '', 'Other Income Expense-286,000-78,000-204,000-1,000-
', '', '-286,000', '-78,000', '-204,000', '-1,000', '', 'Pretax Income28
,490,00024,812,00025,361,00020,594,000-', '28,490,000', '24,812,000', '2
5,361,000', '20,594,000', '', 'Tax Provision4,969,0006,327,0003,249,0004
,660,000-', '4,969,000', '6,327,000', '3,249,000', '4,660,000', '', 'Net Income Common Stockholders23,521,00018,485,00022,111,00015,920,000-', '', '23,521,000', '18,485,000', '22,111,000', '15,920,000', '', 'Average Dilution Earnings-01,00014,000-', '0', '1,000', '14,000', ''

我的目標是在 pandas dataframe 中提取它。 有人可以幫我解決嗎謝謝。

這是使用 yahooquery 的解決方案:

from yahooquery import Ticker

fb = Ticker('fb')
fb.income_statement()

披露:我是 yahooquery 的作者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM