How to web-scrape DL's DT and DD which is under a div with DOMparser/Xpath

我怕爱的太早我们不能终老 提交于 2019-12-25 04:14:10

问题


I am trying to get DL's DT and DD which is under a class and trying to put those in a foreach. But facing some troubles,

<dl class="c-explain2">
        <dt>所在地</dt>
                <dd>
                    大阪府大阪市 北区天満1丁目25番1(地番)
                        <br>

Here is my codes;

$DOMParser = new \DOMDocument();
$DOMParser->loadHTML($html);
$xpath = new \DOMXPath($DOMParser);

$classname="c-explain2";
$getAllTable = $xpath->query("//dl[contains(@class, '$classname')]//");

foreach($getAllTable as $table){
            $allProperties = [];

            $table->getElementsByTagName('dt')[0]->nodeValue;

            $value = $table->getElementsByTagName('dd')[0]->nodeValue;
            $allProperties[] = [
                    'property' => $property, 
                    'value'=> $value];
            }
                $insertData[$start_id] = $allProperties;
                $MyTable = true; 

How to get those dt and dd, After that want to put those in array. Any help? Thank you.


回答1:


There is a problem with your XPath expression, it should be "//dl[@class='$classname']"

Also it looks like you are never assigning $property in your loop. Try this:

<?php
$html = <<<END
<dl class="c-explain2">
        <dt>所在地</dt>
        <dd>大阪府大阪市 北区天満1丁目25番1(地番</dd>
</dl>
END;

$DOMParser = new \DOMDocument();
$DOMParser->loadHTML($html);
$xpath = new \DOMXPath($DOMParser);

$classname   = "c-explain2";
$getAllTable = $xpath->query("//dl[@class='$classname']");

foreach ($getAllTable as $table)
{
    $allProperties = [];

    $property = $table->getElementsByTagName('dt')[0]->nodeValue;

    $value           = $table->getElementsByTagName('dd')[0]->nodeValue;
    $allProperties[] = [
        'property' => $property,
        'value' => $value
    ];
}


来源:https://stackoverflow.com/questions/52601528/how-to-web-scrape-dls-dt-and-dd-which-is-under-a-div-with-domparser-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!