根据特定的alt标记解析并提取图像URL文件名

时间:2016-02-15 20:11:06

标签: php html parsing xpath simple-html-dom

我试图打印除.png扩展名之外的网页中的图片文件扩展名列表。

我只想从仅使用 div class = cartoon 的网站中的图片网址解析所有图片文件名。

示例结构:

<div class="cartoon">
<img src="URL/images/element8/12345.png" alt="cartoon">

所需输出:12345

这是我用来返回所有图片的代码

include('simple_html_dom.php');

$html = new simple_html_dom();
$html->load_file('URL'); 
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("//img"); // find your image
$imageTags = $doc->getElementsByTagName('img');

foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}

1 个答案:

答案 0 :(得分:0)

你想用xpath做吗? 怎么样:

.//*[contains(@class, "cartoon")]//img[not(contains(@src, "png"))]