python-3.x 在刮花的table上用美味的汤吃东西

rvpgvaaj  于 2023-02-26  发布在  Python
关注(0)|答案(1)|浏览(129)

我正试图从marketscreener的“估值”下的第一个表中获取数据。
我发现这个answer非常有用,虽然它打印所有的表,这不一定是一个问题。但我不知道如何访问下面的项目。
例如,我如何获得2024年的市盈率等?
任何建议都会很有帮助!

当前代码

import requests
from bs4 import BeautifulSoup

url = 'https://www.marketscreener.com/quote/stock/MICROSOFT-CORPORATION-4835/financials/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for table in soup.select('table.BordCollapseYear2'):
    # ..scrape every row
    for tr in table.select('tr'):
        row = [td.get_text(strip=True, separator=' ') for td in tr.select('td')]
        print(row)

当前输出:

['Fiscal Period: January', '2020', '2021', '2022', '2023', '2024', '2025']
['Capitalization 1', '12 986', '47 749', '41 428', '26 353', '-', '-']
['Enterprise Value (EV) 1', '12 074', '46 568', '40 171', '24 444', '23 585', '22 437']
['P/E ratio', '-63,6x', '-502x', '-175x', '-148x', '-194x', '-735x']
['Yield', '-', '-', '-', '-', '-', '-']
['Capitalization / Revenue', '27,0x', '54,6x', '28,5x', '11,8x', '8,88x', '6,83x']
['EV / Revenue', '25,1x', '53,3x', '27,7x', '11,0x', '7,95x', '5,81x']
['EV / EBITDA', '-283x', '461x', '168x', '56,0x', '39,1x', '25,7x']
['Enterprise Value (EV) / FCF', '611x', '159x', '90,9x', '37,4x', '26,3x', '18,6x']
['FCF Yield', '0,16%', '0,63%', '1,10%', '2,67%', '3,81%', '5,38%']
['Price to Book', '12,2x', '58,1x', '40,6x', '18,7x', '14,6x', '11,1x']
['Nbr of stocks (in thousands)', '212 567', '221 264', '229 339', '234 394', '-', '-']
['Reference price (USD)', '61,1', '216', '181', '112', '112', '112']
['Announcement Date', '03/19/2020', '03/16/2021', '03/09/2022', '-', '-', '-']
['1 USD in Million', 'Estimates']
['Fiscal Period: January', '2019', '2020', '2021', '2022', '2023', '2024', '2025']
['Net sales 1', '250', '481', '874', '1 452', '2 231', '2 968', '3 861']
['EBITDA 1', '-', '-42,6', '101', '239', '437', '604', '872']
['Operating profit (EBIT) 1', '-', '-65,6', '62,4', '196', '352', '490', '710']
['Operating Margin', '-', '-13,6%', '7,13%', '13,5%', '15,8%', '16,5%', '18,4%']
['Pre-Tax Profit (EBT) 1', '-', '-140', '-87,9', '-160', '-100', '-10,8', '182']
['Net income 1', '-', '-142', '-92,6', '-235', '-176', '-145', '-49,7']
['Net margin', '-', '-29,5%', '-10,6%', '-16,2%', '-7,91%', '-4,90%', '-1,29%']
['EPS 2', '-3,12', '-0,96', '-0,43', '-1,03', '-0,76', '-0,58', '-0,15']
['Free Cash Flow 1', '-', '19,7', '293', '442', '653', '898', '1 207']
['FCF margin', '-', '4,10%', '33,5%', '30,4%', '29,3%', '30,2%', '31,3%']
['FCF Conversion', '-', '-46,3%', '290%', '185%', '150%', '149%', '138%']
['Dividend per Share 2', '-', '-', '-', '-', '-', '-', '-']
['Announcement Date', '05/08/2019', '03/19/2020', '03/16/2021', '03/09/2022', '-', '-', '-']
['1 USD in Million 2 USD', 'Previous period Previous period Next period Next period Estimates', 'Previous period', 'Previous period', 'Next period', 'Next period']
['Previous period', 'Previous period', 'Next period', 'Next period']
['Fiscal Period: January', '2021 Q2', '2021 Q3', '2021 Q4', '2022 Q1', '2022 Q2', '2022 Q3', '2022 Q4', '2023 Q1', '2023 Q2', '2023 Q3', '2023 Q4', '2024 Q1', '2024 Q2', '2024 Q3', '2024 Q4']
['Net sales 1', '199', '232', '265', '303', '338', '380', '431', '488', '535', '581', '627', '666', '717', '765', '819']
['EBITDA 1', '17,2', '29,0', '45,4', '39,3', '44,8', '61,9', '93,2', '95,3', '101', '105', '112', '128', '139', '154', '172']
['Operating profit (EBIT) 1', '7,83', '18,9', '34,4', '29,8', '35,3', '50,7', '80,4', '83,0', '87,3', '89,7', '92,1', '106', '117', '127', '140']
['Operating Margin', '3,93%', '8,15%', '13,0%', '9,83%', '10,5%', '13,3%', '18,7%', '17,0%', '16,3%', '15,4%', '14,7%', '15,9%', '16,3%', '16,6%', '17,1%']
['Pre-Tax Profit (EBT) 1', '-29,4', '-24,1', '-16,2', '-32,8', '-53,1', '-46,0', '-28,2', '-27,0', '-43,5', '-45,8', '-6,50', '-7,70', '-38,4', '12,5', '-11,5']
['Net income 1', '-29,9', '-24,5', '-19,0', '-85,0', '-57,3', '-50,5', '-42,0', '-31,5', '-49,3', '-55,0', '-40,7', '-42,6', '-46,7', '-42,9', '-32,5']
['Net margin', '-15,0%', '-10,6%', '-7,17%', '-28,1%', '-17,0%', '-13,3%', '-9,74%', '-6,46%', '-9,21%', '-9,46%', '-6,49%', '-6,40%', '-6,51%', '-5,61%', '-3,97%']
['EPS 2', '-0,14', '-0,11', '-0,09', '-0,38', '-0,25', '-0,22', '-0,18', '-0,14', '-0,21', '-0,24', '-0,19', '-0,17', '-0,19', '-0,16', '-0,12']
['Dividend per Share 2', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
['Announcement Date', '09/02/2020', '12/02/2020', '03/16/2021', '06/03/2021', '08/31/2021', '12/01/2021', '03/09/2022', '06/02/2022', '08/30/2022', '11/29/2022', '-', '-', '-', '-', '-']
['1 USD in Million 2 USD', 'Previous period Previous period Next period Next period Estimates', 'Previous period', 'Previous period', 'Next period', 'Next period']
['Previous period', 'Previous period', 'Next period', 'Next period']
['Fiscal Period: January', '2019', '2020', '2021', '2022', '2023', '2024', '2025']
['Net Debt 1', '-', '-', '-', '-', '-', '-', '-']
['Net Cash position 1', '-', '912', '1 181', '1 257', '1 909', '2 768', '3 916']
['Leverage (Debt / EBITDA)', '-', '21,4x', '-11,7x', '-5,26x', '-4,37x', '-4,58x', '-4,49x']
['Free Cash Flow 1', '-', '19,7', '293', '442', '653', '898', '1 207']
['ROE (Net Profit / Equities)', '-', '-111%', '7,76%', '16,9%', '29,2%', '29,5%', '30,5%']
["Shareholders' equity 1", '-', '127', '-1 193', '-1 386', '-604', '-493', '-163']
['ROA (Net Profit / Asset)', '-', '-15,4%', '-4,48%', '-7,39%', '-4,08%', '-0,60%', '5,84%']
['Assets 1', '-', '919', '2 069', '3 175', '4 326', '24 220', '-852']
['Book Value Per Share 2', '-', '5,01', '3,71', '4,45', '6,02', '7,70', '10,1']
['Cash Flow per Share 2', '-', '0,68', '1,52', '2,53', '3,41', '4,24', '5,37']
['Capex 1', '-', '80,2', '52,8', '112', '238', '257', '314']
['Capex / Sales', '-', '16,7%', '6,04%', '7,73%', '10,7%', '8,65%', '8,14%']
['Announcement Date', '05/08/2019', '03/19/2020', '03/16/2021', '03/09/2022', '-', '-', '-']
['1 USD in Million 2 USD', 'Previous period Previous period Next period Next period Estimates', 'Previous period', 'Previous period', 'Next period', 'Next period']
['Previous period', 'Previous period', 'Next period', 'Next period']
hgncfbus

hgncfbus1#

一个解决方案可能是从表数据创建各种 Dataframe ,并从中选择值:

import requests
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://www.marketscreener.com/quote/stock/MICROSOFT-CORPORATION-4835/financials/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

all_dfs = []
for table in soup.select('table.BordCollapseYear2'):

    rows = []
    for tr in table.select('tr'):
        row = [td.get_text(strip=True, separator=' ') for td in tr.select('td')]
        rows.append(row)

    df = pd.DataFrame(rows[1:], columns=rows[0])
    df = df.set_index(df.columns[0])
    all_dfs.append(df)

print(all_dfs[0]['2024']['P/E ratio'])  # <-- first dataframe, column "2024", row "P/E ratio"

图纸:

23,5x

相关问题