Получить элементы с помощью xpath и DOMDocument
У меня есть список объявлений в HTML-код ниже. Что мне нужно, это цикл PHP, чтобы получить следующие элементы для каждого объявления:
ad URL (href attribute of<a>
tag)
ad image URL (src attribute of <img>
tag)
ad title (html content of <div class="title">
tag)
<div class="ads">
<a href="http://path/to/ad/1">
<div class="ad">
<div class="image">
<div class="wrapper">
<img src="http://path/to/ad/1/image.jpg">
</div>
</div>
<div class="detail">
<div class="title">Ad #1</div>
</div>
</div>
</a>
<a href="http://path/to/ad/2">
<div class="ad">
<div class="image">
<div class="wrapper">
<img src="http://path/to/ad/2/image.jpg">
</div>
</div>
<div class="detail">
<div class="title">Ad #2</div>
</div>
</div>
</a>
</div>
Мне удалось получить URL объявления с кодом PHP ниже.
$d = new DOMDocument();
$d->loadHTML($ads); // the variable $ads contains the HTML code above
$xpath = new DOMXPath($d);
$ls_ads = $xpath->query('//a');
foreach ($ls_ads as $ad) {
$ad_url = $ad->getAttribute('href');
print("AD URL : $ad_url");
}
Но мне не удалось получить 2 других элемента (URL-адрес изображения и заголовок). Любая идея?