如果你想使用第三方库,你可以使用https://github.com/rajanrx/php-scrape https://github.com/rajanrx/php-scrape
<?php
use Scraper\Scrape\Crawler\Types\GeneralCrawler;
use Scraper\Scrape\Extractor\Types\MultipleRowExtractor;
require_once(__DIR__ . '/../vendor/autoload.php');
date_default_timezone_set('UTC');
// Create crawler
$crawler = new GeneralCrawler('https://coinmarketcap.com/');
// Setup configuration
$configuration = new \Scraper\Structure\Configuration();
$configuration->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);
将打印出以下内容:
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 ...
我希望这对你有帮助:)
免责声明:我是这个库的作者。