SimpleHTMLDOM - 获取iframe src链接

时间:2017-09-27 10:43:13

标签: php iframe simple-html-dom

如何获得" thesrc" iframe标记与SimpleHTMLDOM的链接?

我想要数据的页面来源:

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

?>

它什么都不返回。我做错了什么?

2 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情。 而不是//div[#class='ui-tab-pane']查询div然后查找iframe, 您可以直接使用//div[@class="ui-tab-pane"]/iframe

查询iframe

Try this code snippet here

<?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

答案 1 :(得分:1)

你真的不需要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

相关问题