给定的Html-
<div id="testid">
<h1>Test Title</h1>
<ul class="clearfix">
<li class="anker" id="artists-A"></li>
<li class="first">
<a href="www.test1.html" title="Test1">
<span>
<img src="https://www.test1.de/img/test1.jpg" alt="Test1" />
<span>Test1</span>
</span>
</a>
</li>
<li>
<a href="www.test2.html" title="Test2">
<span>
<img src="https://www.test2.de/img/test2.jpg" alt="Test2" />
<span>Test2</span>
</span>
</a>
</li>
<li class="first">
<a href="www.test3.html" title="Test3">
<span>
<img src="https://www.test1.de/img/test3.jpg" alt="Test3" />
<span>Test3</span>
</span>
</a>
</li>
</ul>
</div>
需要获得a ref值、img src和span ie Title。
我正在使用domDocument分析这个,但没有得到确切的结果。
代码:
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents($url));
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//[@id="testid"]/ul/li');
最佳答案
这里我们使用DOMDocument
。现在我正在收集a
'shref
和img
'ssrc
,您可以添加更多标记。
Try this code snippet here
$domDocument = new DOMDocument();
$domDocument->loadHTML($string);
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query("//div[@id='testid']");//querying div with id="testid"
$results = $domXPath->query("//a|//img",$results->item(0))//querying resultant div for a and img
$data=array();
foreach($results as $result){
if($result->tagName=="a")//checking for anchor tags
{
$data["a"][]=array(
"href"=>$result->getAttribute("href"),
"title"=>$result->getAttribute("title")
);
}
elseif($result->tagName=="img")//checking for image tags
{
$data["img"][]=$result->getAttribute("src");
}
}
print_r($data);
关于php - 我如何使用DomDocument从给定的HTML获取href,Image src,title,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46250874/