Home > Enterprise >  How can I get a value from a table using BeautifulSoup, when that table has multiple values under th
How can I get a value from a table using BeautifulSoup, when that table has multiple values under th

Time:03-24

I am trying to grab the Enterprise Value/EBITDA from yahoo finance. Let's say that our stock is Tesla. https://finance.yahoo.com/quote/TSLA/key-statistics?p=TSLA is the link. Whenever I try to sort by class in BeautifulSoup, I receive the first result in the table. How can I get the Enterprise Value/EBITDA with BeutifulSoup and using HTML tags and such?

Thank you :)

CodePudding user response:

You can get data using pandas and you have to inject user-agent as headers

import pandas as pd
import requests
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'}
url='https://finance.yahoo.com/quote/TSLA/key-statistics?p=TSLA'
table_data =pd.read_html(requests.get(url,headers=headers).text,flavor="bs4")[0]#.to_csv('dat.csv',index=False)
print(table_data)

Output:

        0        1
0      Market Cap (intraday)  822.00B
1           Enterprise Value  813.17B
2               Trailing P/E   162.32
3                Forward P/E    80.65
4  PEG Ratio (5 yr expected)     2.35
5          Price/Sales (ttm)    16.68
6           Price/Book (mrq)    27.23
7   Enterprise Value/Revenue    15.11
8    Enterprise Value/EBITDA    84.48

CodePudding user response:

I would personally

  1. Grab the table as a whole, separate the rows into a list.
  2. Search each row for the string "Enterprise Value/EBITDA"
  • Related