문제

How to get alt attribute from a specific div image by php scraper ? Just look my following code sample. I can print all img "alt" attribute. But I would like to get the "alt" attribute of a specific div class. How can? Here is my code sample:

<?php
error_reporting(E_ALL);
error_reporting(1);
set_time_limit(0);

require 'simple_html_dom.php';
$url = "mylink.com";   
$html = file_get_html($url);
foreach($html->find('img') as $element) 
echo $element->alt.'<br>';
?>
도움이 되었습니까?

해결책

Let's say the div's class is foo:

foreach($html->find('div.foo') as $div) {
  echo $div->alt . '<br>';
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top