【问题标题】:How to get attribute, value and text from tags in array using phpquery如何使用phpquery从数组中的标签中获取属性、值和文本
【发布时间】:2020-10-11 23:27:34
【问题描述】:

我正在尝试使用 PHPQuery 从一个大文件中获取值、文本和超链接,并将其转换为数组。 我已经尝试了一些代码,但在 foreach 循环中将数据从所有 class="hl" 获取到一个数组中时感到困惑。

<?php 
$str ='
<main>
<div class="artfeed ">
<div class="split split_0">
 <div class="split_in">

  <div class="hl" data-id="1036294107">
    <span class="f" country="US"><!-- --></span>
    <div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
     <span class="end"></span> 
     <span class="meta">
      <span class="src" data-pub="DATAPUB">
      <span class="src-part">
      exampleOne.com
      <svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
      </span>
      </span>
      <span class="time" data-time="1592802284">12:04</span>
      </span>
     <a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
    </div>
  </div>

<div class="hl" data-id="1036294107">
    <span class="f" country="US"><!-- --></span>
    <div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
     <span class="end"></span> 
     <span class="meta">
      <span class="src" data-pub="DATAPUB">
      <span class="src-part">
      exampleOne.com
      <svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
      </span>
      </span>
      <span class="time" data-time="1592802284">12:04</span>
      </span>
     <a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
    </div>
  </div>

<div class="hl" data-id="1036294107">
    <span class="f" country="US"><!-- --></span>
    <div class="hl__inner"><a class="hll" href="http://example.com/001/" target="_blank" rel="nofollow">Some of text here</a>
     <span class="end"></span> 
     <span class="meta">
      <span class="src" data-pub="DATAPUB">
      <span class="src-part">
      exampleOne.com
      <svg class="svg-inline--fa fa-cog fa-w-16" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="cog" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg="">
      </span>
      </span>
      <span class="time" data-time="1592802284">12:04</span>
      </span>
     <a class="hl__menu-toggle c-context-menu__btn js-article-menu__toggle" href="#"></a>
    </div>
  </div>

 </div>
</div>
</div>
</main>
';
?>

需要这样的结果:

/*
Array()
Need result: 
Country  : US
href     : http://example.com/001/
Text     : Some of text here
src-part : exampleOne.com
time     : 12:04

Country  : US
href     : http://example.com/001/
Text     : Some of text here
src-part : exampleOne.com
time     : 12:04

Country  : US
href     : http://example.com/001/
Text     : Some of text here
src-part : exampleOne.com
time     : 12:04
*/

我有一些代码

<?php
require("phpQuery.php");
$doc = phpQuery::newDocument($str);
$doc =  $doc['body']->find('main')->find('.artfeed')->find('.hl');
$links = array();
foreach($doc['div'] as $item)
{
 $node = pq($item);
  $sibling = $node->next();
  if ( $sibling->is('a:first') ) {
      $links[] = array(
      $node->attr('country'),
      $sibling->attr('href'),
      $sibling->text(),
    ); 
  } 
}

// Display result:
print_r($links);
?>

【问题讨论】:

  • $links 的当前输出是什么?我立即注意到的一件事是国家/地区设置在跨度上,而不是 div 上,所以我认为这样获取 attr 行不通。
  • 当前输出为空数组,能否在 foreach 循环中提及 class="hl" 类?

标签: php html phpquery


【解决方案1】:

如果您在以下行之后print_r($doc),您是否看到了您希望看到的文档结构?

$doc =  $doc['body']->find('main')->find('.artfeed')->find('.hl');

我之前使用过 Simple HTML Dom,但没有使用过 phpQuery,所以我不确定上述行或其他地方是否有错误。

根据我看到的示例,您应该能够使用 CSS 语法来查找元素。将您的文档更改为以下内容:

$doc =  $doc['body']->find('main')->find('.artfeed');

然后只需使用带有 CSS 语法的 pq() 和 find() 即可直接查找元素而无需循环。

$content = pq($doc);
$links[] = array(
    $content->find('div.hl > span.f')->attr('country'),
    $content->find('div.hl > div.hl__inner > a.hll')->attr('href'),
    $content->find('div.hl > div.hl__inner > a.hll')->text(),
);

编辑:对于多个 hl div,我认为这样的事情可能会起作用:

$doc =  $doc['body']->find('main')->find('.artfeed');
foreach (pq($doc)->find('.hl') as $hl) {
    $links[] = array(
        $hl->find('span.f')->attr('country'),
        $hl->find('div.hl__inner > a.hll')->attr('href'),
        $hl->find('div.hl__inner > a.hll')->text(),
    }
);

【讨论】:

  • 感谢您的宝贵回答。但实际上我在 foreach 循环中需要它,因为我有更多数据 class="hl" 你能帮我更多吗?
  • 我不明白更多数据对于 h1 类意味着什么。您需要的所有数据是在 class="h1" 内还是在更多 h 类元素中?上面的代码是否可以获取那里列出的三个值?请澄清循环的用途并提供尽可能多的信息。我明天早上再看看这个。
  • 是的,上面的代码运行良好,谢谢。我已经更改了问题,请检查一下,我的意思是我有多个class="hl",我想从所有class="hl" 中获取记录并放入数组中。
  • 我根据拥有多个 hl 类修改了答案。让我知道它是否有效。
  • 不工作!错误:DOMElement 类的对象无法转换为字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多