这是file.html
我尝试使用xpath和php循环遍历文档的元素并提取段内的元素并将其添加到数组中并移动到下一个段并将元素提取到下一个数组。 / p>
<html>
<body>
<div class="columns">
<div width="90">
<div class="item">
<center>Yes
<br />
<a class="Link" href="https://google.com"><img src="http://img.jpg" /></a>
</center>
</div>
<p class="Icon">
<a class="Link" href="https://google.com"><strong class="Title">Anchor Text</strong></a><br />
<a class="secondLink"target="_blank">Anchor Text 1</a><br />
<strong class="price"><a class="rice">Anchor Text 2</a></strong>
</p>
</div>
<div width="90">
<div class="item">
<center>No
<br />
<a class="Link" href="https://bing.com"><img src="http://img.jpg" /></a>
</center>
</div>
<p class="Icon">
<a class="Link" href="https://bing.com"><strong class="Title">Anchor Text 2</strong></a><br />
<a class="secondLink"target="_blank">Anchor Text 2 1</a><br />
<strong class="price"><a class="rice">Anchor Text 2 2</a></strong>
</p>
</div>
</div>
</body>
这是PHP代码
<?php
$html = file_get_contents('file.html');
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html)){
$doc->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$scrape = $xpath->query("//div[@width='90']");
if($scrape->length > 0){
foreach($scrape as $row){
$anames = array();
$names = $xpath->query("//strong[@class='Title']", $row);
foreach ($names as $name){
$anames[] = $name->nodeValue;
}
$list[] = array('type' => $type, 'name' => $anames);
}
print_r($list);
}
}
?>
但我无法从强标记中提取名称。我收到以下错误
**注意:尝试获取非对象**的属性
这是输出:
Array
(
[0] => Array
(
[type] =>
Yes
[name] => Array
(
[0] => Anchor Text
[1] => Anchor Text 2
)
)
[1] => Array
(
[type] =>
No
[name] => Array
(
[0] => Anchor Text
[1] => Anchor Text 2
)
)
)
我希望能够提取名称并为每个数组分配一个名称。作为&#34;锚文本&#34;到第一个数组和&#34;锚文本2&#34;第二阵列。