【问题标题】:PHP extract specific DIV from target URLPHP 从目标 URL 中提取特定的 DIV
【发布时间】:2014-03-31 05:05:21
【问题描述】:

我正在使用 Simple HTML DOM 尝试从目标 URL 中提取 div 及其所有内容,这是我的代码:

<?php
  require 'simple_html_dom.php';
  $html = file_get_html('http://mozilla.org');
  foreach($html->find('.accordion') as $element) 
      echo $element . '<br>';
?>

我遇到的问题是上面的代码只提取了 div 的纯文本。 div中还有我需要提取的图像。如果我使用以下代码,则所有图像都会被提取,但页面中的其他所有内容也会被提取。

<?php
  require 'simple_html_dom.php';
  $html = file_get_html('http://mozilla.org');
  echo $html;
?>

所以我的问题是,如何使用第一段代码从.accordion 中提取内容+图像?

谢谢

【问题讨论】:

标签: php dom simple-html-dom


【解决方案1】:

你总是可以尝试的;

$imgs = array();
foreach($html->find('.accordion',0)->find('img') as $img){
    $imgs[] = $img->src;
}

print_r($imgs);

这应该使用来自.accordion div 的所有图像链接填充$imgs 变量。

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2019-10-11
    • 2015-01-20
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多