2017-09-27 171 views
0

如何使用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; 
} 

?> 

它沒有返回。我做錯了什麼?

回答

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

+0

的最佳解決方案,它的工作完美無瑕!謝謝:) – Jack

+0

非常好!很高興我能幫到 – delboy1978uk

+0

@Jack這將在'DOM'中獲得所有'iframe'。如果這是你想要的,那麼這是一個完美的解決方案。 –

1

你可以嘗試這樣的事情。 而不是查詢//div[#class='ui-tab-pane']div,然後找到iframe, 可以直接查詢IFRAME與//div[@class="ui-tab-pane"]/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