【问题标题】:phpQuery find idphpQuery查找id
【发布时间】:2016-11-24 18:00:57
【问题描述】:

我正在使用 phpQuery 来解析网站:

HTML 示例:

<div class="post" id="1232323">
   <div class="title">Example</div>
   <div class="text">texttexttext</div>
</div>

我可以找到标题和文字,但在 div.post 中找不到 id。

$document = phpQuery::newDocument(takeCode("example.com"));
$list_elements = $document->find('div.Post');
foreach ($list_elements as $element) 
{
   $pq = pq($element);
   $postTitle = $pq->find('div.Post')->attr('id'); //not found
   $postTitle = $pq->find('div.title')->text(); //found
   $postTitle = $pq->find('div.content')->text(); //found

}

我该如何解决?

【问题讨论】:

  • $pq-&gt;attr('id') 不给你那个 id 吗?
  • 谢谢。 $pq->attr('id')
  • 如果它适合你,我可以把它变成一个答案吗?

标签: php phpquery


【解决方案1】:
foreach ($list_elements as $element) 
{
   $pq = pq($element);
   $postTitle = $pq->attr('id') //!
   $postTitle = $pq->find('div.title')->text(); //found
   $postTitle = $pq->find('div.content')->text(); //found

}

【讨论】:

    【解决方案2】:

    要获取 ID,请使用:

     $pq->attr('id');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多