2016-11-24 37 views
1

我使用phpQuery分析一个网站:phpQuery找到ID

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

是否'$ PQ-> ATTR( 'ID')'不给你这个ID? – Stuart

+0

谢谢。 $ pq-> attr('id') – dayMe

+0

如果它适合你,我应该把它变成一个答案吗? – Stuart

回答

0

要获得ID,请使用:

$pq->attr('id'); 
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 

}