php scrapping and outputting a specific value or number in a given tag

前端 未结 2 1370
逝去的感伤
逝去的感伤 2021-01-28 14:31

so I\'m very new to php. But with some help, I\'ve figured out how to scrape a site if it has a tag identifier like h1 class=____

And even better, I\'ve figured out how

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 14:41

    If you want to use third party library you can use https://github.com/rajanrx/php-scrape

    setTargetXPath('//table[@id="currencies"]');
    $configuration->setRowXPath('.//tbody/tr');
    $configuration->setFields(
        [
            new \Scraper\Structure\TextField(
                [
                    'name'  => 'Name',
                    'xpath' => './/td[2]/a',
                ]
            ),
            new \Scraper\Structure\TextField(
                [
                    'name'  => 'Market Cap',
                    'xpath' => './/td[3]',
                ]
            ),
            new \Scraper\Structure\RegexField(
                [
                    'name'  => '% Change',
                    'xpath' => './/td[7]',
                    'regex' => '/(.*)%/'
                ]
            ),
        ]
    );
    
    // Extract  data
    $extractor = new MultipleRowExtractor($crawler, $configuration);
    $data = $extractor->extract();
    print_r($data);
    

    will print out following:

    Array
    (
        [0] => Array
            (
                [Name] => Bitcoin
                [Market Cap] => $42,495,710,233
                [% Change] => -1.09
                [hash] => 76faae07da1d2f8c1209d86301d198b3
            )
    
        [1] => Array
            (
                [Name] => Ethereum
                [Market Cap] => $28,063,517,955
                [% Change] => -8.10
                [hash] => 18ade4435c69b5116acf0909e174b497
            )
    
        [2] => Array
            (
                [Name] => Ripple
                [Market Cap] => $11,483,663,781
                [% Change] => -2.73
                [hash] => 5bf61e4bb969c04d00944536e02d1e70
            )
    
        [3] => Array
            (
                [Name] => Litecoin
                [Market Cap] => $2,263,545,508
                [% Change] => -3.36
                [hash] => ea205770c30ddc9cbf267aa5c003933e
            )
       and so on ...
    

    I hope that helps you :)

    Disclaimer: I am author of this library.

提交回复
热议问题