我正在尝试在网页中打印出图像文件扩展名列表,不包括.png扩展名。
我只想从仅使用 div class = 卡通的网站中的图像 url 解析所有图像文件名。
示例结构:
<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');
}
你想用 xpath 来做吗?怎么样:
.//*[contains(@class, "cartoon")]//img[not(contains(@src, "png"))]