使用 DOMDocument 从 HTML 页面抓取数据

Scrape data from HTML page using DOMDocument

我是 PHP 的新手,正在尝试制作可以从外部站点获取数据的脚本。我对获得欧宝 Merk 的价值很感兴趣。 HTML 代码如下

<div class="row">
    <div class="col-6 col-sm-5 label" data-tooltip="<strong>Merk</strong><br/>Het merk van het voertuig. Dit wordt voor alle voertuigsoorten geregistreerd.
<span>bron: RDW</span> ">
        Merk
    <span data-toggle="tooltip" data-html="true" title="<strong>Merk</strong><br/>Het merk van het voertuig. Dit wordt voor alle voertuigsoorten geregistreerd.<br /><span>bron: RDW</span> "></span><span data-toggle="tooltip" data-html="true" title="<strong>Merk</strong><br/>Het merk van het voertuig. Dit wordt voor alle voertuigsoorten geregistreerd.<br /><span>bron: RDW</span> "></span></div>
    <div class="col-6 col-sm-7 value">
Opel
    </div>
</div>

我正在尝试使用如下 PHP 代码获取它

<?php
// a new dom object
$dom = new domDocument; 

// load the html into the object
$dom->loadHTML('https://centraalbeheerkentekencheck.azurewebsites.net/?kenteken=L-762-LZ'); 

// discard white space
$dom->preserveWhiteSpace = false;

$rowData= $dom->getElementsByTagName('row');

但现在我被困住了,不知道如何完成剩余代码,以便获得欧宝的 Merk 的价值。让我知道这里是否有人可以帮助我实现我的目标。

我认为为此使用 SimpleHtmlDom 更好(比如 voku/simple_html_dom):

composer install voku/simple_html_dom

SimpleHtmlDom 版本

你为此使用了 url https://centraalbeheerkentekencheck.azurewebsites.net/?kenteken=L-762-LZ,但它包含一个 iframe 到:https://centraalbeheer.finnik.nl/kenteken/l762lz/gratis,所以我在脚本中改用那个:

use voku\helper\HtmlDomParser;
require_once __DIR__ . "/vendor/autoload.php";

function getBrand(string $license) : string
{
    $license = strtolower(str_replace("-", "", $license));
    $dom = HtmlDomParser::file_get_html("https://centraalbeheer.finnik.nl/kenteken/".$license."/gratis");
    $brand = $dom->find(".result .row .value")[0]->innerHtml();
    return str_replace(["&#13;", "\n", "\r"], "", $brand);
}

var_dump(getBrand("L-762-LZ"));

更新:您也可以使用正则表达式

function getBrandRegex(string $license) : string
{
    $license = strtolower(str_replace("-", "", $license));
    $content = file_get_contents("https://centraalbeheer.finnik.nl/kenteken/".$license."/gratis");
    preg_match_all('/<div class="col-6 col-sm-7 value">(.*?)<\/div>/s', $content, $matches);
    $brand = $matches[1][0];
    return trim(str_replace(["&#13;", "\n", "\r"], "", $brand));
}

var_dump(getBrandRegex("L-762-LZ"));

更新:DomDocument 版本

function getBrandDomDocument(string $license) : string
{
    libxml_use_internal_errors(true); //see: https://www.php.net/manual/en/function.libxml-use-internal-errors.php
    $license = strtolower(str_replace("-", "", $license));
    $dom = new \DomDocument;
    $dom->loadHTMLFile("https://centraalbeheer.finnik.nl/kenteken/".$license."/gratis");
    $dom->preserveWhiteSpace = false;

    $xpath = new \DOMXPath($dom);
    $data = $xpath->query("//div[contains(@class, 'col-6 col-sm-7 value')]");

    return trim(str_replace(["&#13;", "\n", "\r"], "", $data[0]->textContent));
}

var_dump(getBrandDomDocument("L-762-LZ"));

输出

Opel