【问题标题】:Using pd.read_html to return a specific table from a webpage of multiple tables使用 pd.read_html 从多个表的网页返回特定表
【发布时间】:2022-02-08 22:13:48
【问题描述】:

我正在尝试从此webpage 返回一个特定表。

我曾尝试使用 beautifulsoup 来抓取它,但这太复杂了,所以我尝试使用 pd.read_html 代替。我要的表格是带有“XYZ 大股东”的表格(请注意,这只是一个特定页面,我也将使用此代码返回其他股票的表格)。

这是我目前用于搜索表格的代码 - 我最初的想法是在我所追求的标题的标题中找到“Holding”这个词,但它还拿起了另一张我不想要的桌子,因为“Holdings”这个词出现在之前的一张桌子上。


# Scrape the Substantial Shareholder table if it exists
sub_table = pd.read_html(current_url, match='Holding')
print(sub_table)

电流输出

                    Company   Mkt Cap         2018 A  2019 A   2020 A    2018 A     2019 A    2020 A             2018 A 2019 A 2020 A
0              Altium (ALU)   $4,528M         0.1377  0.3989   0.2353   96.9595    69.3097   56.1095              1.55%  1.28%  1.46%
1  Brainchip Holdings (BRN)   $2,974M         0.6838  0.0000   0.0000    0.0000     0.0000    0.0000              0.00%  0.00%  0.00%
2      Technology One (TNE)   $3,299M         0.1484  0.1679   0.1574   45.3375    38.8213   33.5414              1.36%  1.59%  1.84%
3     Wisetech Global (WTC)  $14,241M         1.2490  0.3624   0.2610  124.5434    91.4118   72.4917              0.15%  0.20%  0.30%
4                Xero (XRO)  $16,610M         2.6697 -0.8109  18.9607  920.3138  4866.8122  243.8197              0.00%  0.00%  0.00%,                 Holding                                        Name
0  176,305,508 (10.82%)                          Peter Van Der Made
1   119,205,377 (7.31%)  Merrill Lynch (Australia) Nominees Pty Ltd
2      114,093,915 (7%)   HSBC Custody Nominees (Australia) Limited]

Process finished with exit code 0

期望的输出

Holding                                        Name
0  176,305,508 (10.82%)                          Peter Van Der Made
1   119,205,377 (7.31%)  Merrill Lynch (Australia) Nominees Pty Ltd
2      114,093,915 (7%)   HSBC Custody Nominees (Australia) Limited]

有没有办法让我专门匹配只看标题而不是整个页面内容?

【问题讨论】:

    标签: python html pandas web-scraping


    【解决方案1】:

    您只需要这样做:

    current_url = 'https://www.intelligentinvestor.com.au/shares/asx-brn/brainchip-holdings-ltd'
    
    sub_table = pd.read_html(current_url, match='Holding')
    print(sub_table[1])
    

    返回:

              Holding                                        Name
    0  176,305,508 (10.82%)                          Peter Van Der Made
    1   119,205,377 (7.31%)  Merrill Lynch (Australia) Nominees Pty Ltd
    2      114,093,915 (7%)   HSBC Custody Nominees (Australia) Limited
    ​
    

    同时

    current_url = 'https://www.intelligentinvestor.com.au/shares/asx-brn/brainchip-holdings-ltd'
    
    sub_table = pd.read_html(current_url, match='Holding')
    print(sub_table[0])
    

    给予

                   Company   Mkt Cap EPS Growth (%)                   \
                        Company   Mkt Cap         2018 A  2019 A   2020 A   
    0              Altium (ALU)   $4,536M         0.1377  0.4001   0.2353   
    1  Brainchip Holdings (BRN)   $2,794M         0.6838  0.0000   0.0000   
    2      Technology One (TNE)   $3,296M         0.1484  0.1679   0.1574   
    3     Wisetech Global (WTC)  $14,456M         1.2490  0.3624   0.2610   
    4                Xero (XRO)  $16,684M         2.6697 -0.8101  18.8826   
    
        P/E (%)                      Dividend Yield (%)                
         2018 A     2019 A    2020 A             2018 A 2019 A 2020 A  
    0   97.1284    69.3746   56.1615              1.54%  1.28%  1.46%  
    1    0.0000     0.0000    0.0000              0.00%  0.00%  0.00%  
    2   45.2931    38.7833   33.5085              1.36%  1.60%  1.84%  
    3  126.4269    92.7943   73.5880              0.15%  0.20%  0.29%  
    4  924.4426  4867.3913  244.8065              0.00%  0.00%  0.00%  
    ​
    

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 2022-12-16
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      相关资源
      最近更新 更多