【问题标题】:Get IFrame src with SimpleHTMLDOM parser使用 SimpleHTMLDOM 解析器获取 IFrame src
【发布时间】:2015-01-18 11:03:51
【问题描述】:

您好,我正在研究 scraper,但我无法获得任何信息。

这是链接http://sfglobe.com/?id=19110

div class="video_container">
<div class="video_object">
<iframe id="player" width="100%" height="100%" frameborder="0" allowfullscreen="1" title="YouTube video player" 
src="http://www.youtube.com/embed/KMYrIi_Mt8A?enablejsapi=1&controls=1&showinfo=0& color=white&rel=0&wmode=transparent&modestbranding=1&theme=light&autohide=1&start=4& origin=http%3A%2F%2Fsfglobe.com">
<!DOCTYPE html>
<html lang="en" data-cast-api-enabled="true" dir="ltr"

我需要 src="http://www.youtube.com/embed/KMYrIi_Mt8A....."

这是我的代码,它不起作用

foreach ($html->find('.video_object')as $iframe){
echo "this is video ".$iframe->outertext  ." <br>";
}

非常感谢你

【问题讨论】:

  • ('.video_object iframe')-&gt;src?
  • foreach ($html->find('.video_object iframe')as $iframe){ echo "这是视频 ".$iframe->src ."
    ";我做了这个它没有用

标签: php html parsing dom


【解决方案1】:

这会在您的代码中返回任何内容吗?

$html->find('.video_object iframe')

如果是这样,请尝试使用-&gt;getAttribute('src');,它可能会起作用。

更多信息请查看PHP DOMElement


编辑

改用XPath,它会输出预期的结果

//init DOMDocument
$dom = new DOMDocument();
//get the source from the URL
$html = file_get_contents("URL");
//load the html from html string
$dom->loadHTML($html);
//init XPath
$xpath = new DOMXPath($dom);

//fetch the src from the iframe within
$iframe_src=$xpath->query('//*[@class="CLASSNAME"]/iframe//@src');

vardump($iframe_src);

【讨论】:

  • 我遇到了类似的问题。 getAttribute() 不起作用。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多