简体   繁体   English

SimpleHTMLDOM-获取iframe src链接

[英]SimpleHTMLDOM - Get iframe src link

How to get the "thesrc" link from the iframe tag with SimpleHTMLDOM? 如何使用SimpleHTMLDOM从iframe标记获取“ thesrc”链接?

The source of the page i want the data from: 页面的来源我想从中获取数据:

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

So i tried: 所以我试着:

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

?>

It returns nothing. 它什么也不返回。 What i'm doing wrong? 我做错了什么?

You can try something like this. 您可以尝试这样。 Instead of querying //div[#class='ui-tab-pane'] over div and then finding iframe , You can query iframe directly with //div[@class="ui-tab-pane"]/iframe 而不是查询的//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"));

Output: 输出:

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

You don't really need the XPath stuff to achieve this. 您实际上并不需要XPath的东西来实现这一目标。 Have a look here: 在这里看看:

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

echo $src;

Which gives you: 这给你:

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

See it working here https://3v4l.org/41CRj 看到它在这里工作https://3v4l.org/41CRj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM