【问题标题】:Parse and extract image URL file names based on specific alt tags根据特定的 alt 标签解析和提取图像 URL 文件名
【发布时间】:2016-02-15 20:11:05
【问题描述】:

我正在尝试打印网页中的图像文件扩展名列表,不包括 .png 扩展名。

我只想解析网站内图片网址中的所有图片文件名,该网站仅使用 div class= 卡通。

示例结构:

<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');
}

【问题讨论】:

  • 对PHP不熟悉,但是$nodelist和$imageTags里面好像有一样的?你为什么要两者兼得?你的代码有什么问题?什么没有按预期工作?

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


【解决方案1】:

你想用 xpath 来做吗? 怎么样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2013-04-22
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多