2017-09-27 15 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 이상 //div[#class='ui-tab-pane']를 조회 한 다음에 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