SimpleHTMLDOM - 获取 iframe src link

SimpleHTMLDOM - Get iframe src link

如何使用 SimpleHTMLDOM 从 iframe 标记中获取 "thesrc" link?

我要获取数据的页面来源:

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

?>

它returns没什么。我做错了什么?

你可以尝试这样的事情。 而不是在 div 上查询 //div[#class='ui-tab-pane'] 然后找到 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

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