SimpleHTMLDOM - 获取iframe src链接
问题描述:
如何使用SimpleHTMLDOM从iframe标记中获取“thesrc”链接?SimpleHTMLDOM - 获取iframe src链接
页面的源,我想从数据:
<div class="ui-tab-pane" data-role="panel" id="feedback">
<iframe scrolling="no" frameborder="0" marginwidth="0" marginheight="0"
width="100%" height="200"
thesrc="//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true"></iframe>
</div>
所以我尝试:
<?php
include_once('simple_html_dom.php');
$html = file_get_html('https://www.aliexpress.com/item/Global-Version-Xiaomi-Redmi-Note-4-Mobile-Phone-3GB-RAM-32GB-ROM-Snapdragon-625-Octa-Core/32795263337.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//div[#class='ui-tab-pane']") as $node){
echo $node->getElementsByTagName('iframe')[0]->getAttribute("thesrc");
echo $node->getElementsByTagName('div')[0]->nodeValue;
}
?>
它没有返回。我做错了什么?
答
你并不真正需要的XPath的东西来实现这一点。看看这里:
$dom = new DOMDocument();
$dom->loadHTML($html);
$iFrame = $dom->getElementsByTagName('iframe')->item(0);
$src = $iFrame->getAttribute('thesrc');
echo $src;
它给你:
//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true
看到它在这里工作https://3v4l.org/41CRj
答
你可以尝试这样的事情。 而不是查询//div[#class='ui-tab-pane']
了div
,然后找到iframe
, 可以直接查询IFRAME与//div[@class="ui-tab-pane"]/iframe
<?php
ini_set('display_errors', 1);
libxml_use_internal_errors(true);
$string=<<<STR
<div class="ui-tab-pane" data-role="panel" id="feedback">
<iframe scrolling="no" frameborder="0" marginwidth="0" marginheight="0" width="100%" height="200" thesrc="//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true"></iframe>
</div>
STR;
$domDocument = new DOMDocument();
$domDocument->loadHTML($string);
$domXPath = new DOMXPath($domDocument);
$results = $domXPath->query('//div[@class="ui-tab-pane"]/iframe');
print_r($results->item(0)->getAttribute("thesrc"));
输出:
//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true
的最佳解决方案,它的工作完美无瑕!谢谢:) – Jack
非常好!很高兴我能帮到 – delboy1978uk
@Jack这将在'DOM'中获得所有'iframe'。如果这是你想要的,那么这是一个完美的解决方案。 –