Home > Net >  How to select a table in web page with multiple tables from the same class using BeautifulSoup
How to select a table in web page with multiple tables from the same class using BeautifulSoup

Time:11-19

I want to get the "Tesla Quarterly Revenue" Table data from "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue" .There are2 tables with same table class (table ) . So I tried to get the table index for the required table but it gives me an error. Appreciate your help to sort this out. Thank you

url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
    data  = requests.get(url).text 
    soup = BeautifulSoup(data,"html5lib")
    tables = soup.find('table')
    # find table with "Tesla Quarterly Revenue"
    for index,table in enumerate(tables):
        if ("Tesla Quarterly Revenue" in str(table)):
            table_index = index
    
    print(tables[table_index].prettify())

CodePudding user response:

try these:

import pandas as pd
df=pd.read_html("https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue")
print(df)

output:

[    Tesla Annual Revenue(Millions of US $) Tesla Annual Revenue(Millions of US $).1
0                                     2020                                  $31,536
1                                     2019                                  $24,578
2                                     2018                                  $21,461
3                                     2017                                  $11,759
4                                     2016                                   $7,000
5                                     2015                                   $4,046
6                                     2014                                   $3,198
7                                     2013                                   $2,013
8                                     2012                                     $413
9                                     2011                                     $204
10                                    2010                                     $117
11                                    2009                                     $112
12                                    2008                                      $15,    Tesla Quarterly Revenue(Millions of US $) Tesla Quarterly Revenue(Millions of US $).1
0                                 2021-09-30                                     $13,757
1                                 2021-06-30                                     $11,958
2                                 2021-03-31                                     $10,389
3                                 2020-12-31                                     $10,744
4                                 2020-09-30                                      $8,771
5                                 2020-06-30                                      $6,036
6                                 2020-03-31                                      $5,985
7                                 2019-12-31                                      $7,384
8                                 2019-09-30                                      $6,303
9                                 2019-06-30                                      $6,350
10                                2019-03-31                                      $4,541
11                                2018-12-31                                      $7,226
12                                2018-09-30                                      $6,824
13                                2018-06-30                                      $4,002
14                                2018-03-31                                      $3,409
15                                2017-12-31                                      $3,288
16                                2017-09-30                                      $2,985
17                                2017-06-30                                      $2,790
18                                2017-03-31                                      $2,696
19                                2016-12-31                                      $2,285
20                                2016-09-30                                      $2,298
21                                2016-06-30                                      $1,270
22                                2016-03-31                                      $1,147
23                                2015-12-31                                      $1,214
24                                2015-09-30                                        $937
25                                2015-06-30                                        $955
26                                2015-03-31                                        $940
27                                2014-12-31                                        $957
28                                2014-09-30                                        $852
29                                2014-06-30                                        $769
30                                2014-03-31                                        $621
31                                2013-12-31                                        $615
32                                2013-09-30                                        $431
33                                2013-06-30                                        $405
34                                2013-03-31                                        $562
35                                2012-12-31                                        $306
36                                2012-09-30                                         $50
37                                2012-06-30                                         $27
38                                2012-03-31                                         $30
39                                2011-12-31                                         $39
40                                2011-09-30                                         $58
41                                2011-06-30                                         $58
42                                2011-03-31                                         $49
43                                2010-12-31                                         $36
44                                2010-09-30                                         $31
45                                2010-06-30                                         $28
46                                2010-03-31                                         $21
47                                2009-12-31                                         NaN
48                                2009-09-30                                         $46
49                                2009-06-30                                         $27
50                                2008-12-31                                         NaN,                                               Sector  ...                                            Revenue
0                                  Auto/Tires/Trucks  ...                                           $31.536B
1  Tesla Inc. designs, develops, manufactures, an...  ...  Tesla Inc. designs, develops, manufactures, an...

[2 rows x 4 columns],               Stock Name        Country Market Cap  PE Ratio
0    General Motors (GM)  United States   $90.901B      8.16
1         Ford Motor (F)  United States   $79.086B     10.58
2          Polaris (PII)  United States    $7.596B     12.13
3              IAA (IAA)  United States    $7.311B     23.89
4  Harley-Davidson (HOG)  United States    $5.960B     10.11,                      Link Preview  HTML Code (Click to Copy)
0  Tesla Revenue 2009-2021 | TSLA                        NaN
1                     Macrotrends                        NaN
2                          Source                        NaN,                      Link Preview  HTML Code (Click to Copy)
0  Tesla Revenue 2009-2021 | TSLA                        NaN
1                     Macrotrends                        NaN
2                          Source                        NaN]

CodePudding user response:

Oh I found the error ..Silly mistake I should have used tables = soup.find_all('table') instead of tables = soup.find('table')

  • Related