【问题标题】:simple html dom parser -> find "conditions"简单的 html dom 解析器 -> 查找“条件”
【发布时间】:2018-02-21 23:17:19
【问题描述】:

我在使用简单的 html dom 解析器从网站解析时遇到问题。我尝试解析这样的代码:

<li>
 <p class="x"></p><p>..</p> <p>..</p> <p>..</p>
 <p class="x"></p><p>..</p> <p>..</p> <p>..</p>
 <p class="x"></p><p>..</p> <p>..</p> <p>..</p>
</li>

我的目标是将这些段落分开保存。 对于具有类定义的段落很容易,例如$year = $class-&gt;find(p[class=x]');,但我也需要解析其他段落。如何仅将没有类规范的段落保存到另一个数组中,而没有带类的段落?

【问题讨论】:

  • 您使用什么语言? PHP?
  • 是的,包括 ("simple_html_dom.php")

标签: php html parsing dom


【解决方案1】:

我不认为你可以在find 方法中做到这一点,所以过滤后:

$year = $class->find('p');
// filter out nodes with empty class properties
$without = array_filter($year, function($v) { return empty($v->class); });

// since array_filter preserves keys
$with = array_diff_key($year, $without);

// or filter again checking that class is NOT empty
$with = array_filter($year, function($v) { return !empty($v->class); });

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多