2017-09-27 6 views
0

Comment obtenir le lien "thesrc" de la balise iframe avec SimpleHTMLDOM?SimpleHTMLDOM - Obtenir iframe src link

La source de la page je veux les données:

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

donc j'ai essayé:

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

?> 

Il ne retourne rien. Qu'est-ce que je fais mal?

Répondre

1

Vous ne avez pas vraiment besoin de la XPath choses pour y parvenir. Jetez un coup d'oeil ici:

$dom = new DOMDocument(); 
$dom->loadHTML($html); 
$iFrame = $dom->getElementsByTagName('iframe')->item(0); 
$src = $iFrame->getAttribute('thesrc'); 

echo $src; 

qui vous donne:

//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true 

voir travailler ici https://3v4l.org/41CRj

+0

solution Grand et il fonctionne sans faille! Merci :) – Jack

+0

Excellent! Heureux je pourrais aider – delboy1978uk

+0

@Jack Cela obtiendra tout 'iframe' avec dans le' DOM'. Si c'est ce que vous voulez, c'est la solution parfaite. –

1

Vous pouvez essayer quelque chose comme ça. Au lieu de demander //div[#class='ui-tab-pane'] sur div puis trouver iframe, Vous pouvez interroger iframe directement avec //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")); 

Sortie:

//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true