[英]SimpleHTMLDOM - Get iframe src link
如何使用SimpleHTMLDOM从iframe标记获取“ thesrc”链接?
页面的来源我想从中获取数据:
<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;
}
?>
它什么也不返回。 我做错了什么?
您可以尝试这样。 而不是查询的//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
您实际上并不需要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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.