我正在使用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
}
我该如何解决?
答案 0 :(得分: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
}
答案 1 :(得分:0)
要获取ID,请使用:
$pq->attr('id');