How can I select from only one table with Web::Scraper?

后端 未结 3 1214
梦毁少年i
梦毁少年i 2021-01-25 06:19

I want to extract the text only for heading Node Object Methods from a webpage. The specific HMTL part is as follows:

Node Object Properties

3条回答
  •  无人共我
    2021-01-25 06:34

    Web::Scraper can use nth_of_type to choose the right table. There are two tables with the same class, so you can say table.reference:nth-of-type(2):

    use v5.22;
    
    use feature qw(postderef);
    no warnings qw(experimental::postderef);
    
    
    use Web::Scraper;
    
    my $html = do { local $/;  };
    
    my $methods = scraper {
        process "table.reference:nth-of-type(2) > tr > td > a", 'renners[]' => 'TEXT';
        };
    my $res = $methods->scrape( $html );
    
    say join "\n", $res->{renners}->@*;
    

    And here's a Mojo::DOM:

    use Mojo::DOM;
    
    my $html = do { local $/;  };
    
    my $dom = Mojo::DOM->new( $html );
    
    say $dom
        ->find( 'table.reference:nth-of-type(2) > tr > td > a' )
        ->map( 'text' )
        ->join( "\n" );
    

    I tried looking for a selector solution that could recognize the text in the h2, but my kung fu is weak here.

提交回复
热议问题