问题
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