2009-06-06 56 views
12

我是新來的DOM解析PHP:
我有一個HTML文件,我想解析。它有一堆這樣的DIVs:如何使用DOM解析器

<div id="interestingbox"> 
    <div id="interestingdetails" class="txtnormal"> 
     <div>Content1</div> 
     <div>Content2</div> 
    </div> 
</div> 

<div id="interestingbox"> 
...... 

我想要使用php的許多div框的內容。 如何使用DOM解析器來執行此操作?

謝謝!

回答

20

首先我要告訴你,你不能用相同的ID在兩個不同的div;有關於這一點的課程。每個元素都應該有一個唯一的ID。

代碼來獲取div的內容與ID = 「interestingbox」

$html = ' 
<html> 
<head></head> 
<body> 
<div id="interestingbox"> 
    <div id="interestingdetails" class="txtnormal"> 
     <div>Content1</div> 
     <div>Content2</div> 
    </div> 
</div> 

<div id="interestingbox2"><a href="#">a link</a></div> 
</body> 
</html>'; 


$dom_document = new DOMDocument(); 

$dom_document->loadHTML($html); 

//use DOMXpath to navigate the html with the DOM 
$dom_xpath = new DOMXpath($dom_document); 

// if you want to get the div with id=interestingbox 
$elements = $dom_xpath->query("*/div[@id='interestingbox']"); 

if (!is_null($elements)) { 

    foreach ($elements as $element) { 
    echo "\n[". $element->nodeName. "]"; 

    $nodes = $element->childNodes; 
    foreach ($nodes as $node) { 
     echo $node->nodeValue. "\n"; 
    } 

    } 
} 

//OUTPUT 
[div] { 
     Content1 
     Content2 
} 

示例使用類:

$html = ' 
<html> 
<head></head> 
<body> 
<div class="interestingbox"> 
    <div id="interestingdetails" class="txtnormal"> 
     <div>Content1</div> 
     <div>Content2</div> 
    </div> 
</div> 

<div class="interestingbox"><a href="#">a link</a></div> 
</body> 
</html>'; 

//the same as before.. just change the xpath 

[...] 

$elements = $dom_xpath->query("*/div[@class='interestingbox']"); 

[...] 

//OUTPUT 
[div] { 
     Content1 
     Content2 
} 

[div] { 
a link 
} 

參考DOMXPath頁瞭解詳情。

6

我得到這個使用simplehtmldom作爲開始工作:

$html = file_get_html('example.com'); 
foreach ($html->find('div[id=interestingbox]') as $result) 
{ 
    echo $result->innertext; 
} 
+0

這是非常容易使用 – 2013-08-02 12:53:13

0

http://www.sitepoint.com/forums/showthread.php?611393-php5-need-something-like-innerHTML-instead-of-nodeValue

function innerXML($node) 

{ 

    $doc = $node->ownerDocument; 

    $frag = $doc->createDocumentFragment(); 

    foreach ($node->childNodes as $child) 

    { 

     $frag->appendChild($child->cloneNode(TRUE)); 

    } 

    return $doc->saveXML($frag); 

} 


$dom = new DOMDocument(); 

$dom->loadXML(' 

<html> 

<body> 

<table> 

<tr> 

    <td id="foo"> 

     The first bit of Data I want 

     <br />The second bit of Data I want 

     <br />The third bit of Data I want 

    </td> 

</tr> 

</table> 

<body> 

<html> 



'); 

$xpath = new DOMXPath($dom); 

$node = $xpath->evaluate("/html/body//td[@id='foo' ]"); 

$dataString = innerXML($node->item(0)); 
$dataArr = explode("<br />", $dataString); 

$dataUno = $dataArr[0]; 
$dataDos = $dataArr[1]; 
$dataTres = $dataArr[2]; 

echo "firstdata = $nameUno<br />seconddata = $nameDos<br />thirddata = $nameTres<br />" 
0

WebExtractor非常好的功能:https://github.com/knyga/webextractor 它可以使用CSS,正則表達式,XPath選擇解析頁面。

查找包和試驗實施例:

使用WebExtractor \ DataExtractor \ DataExtractorFactory;使用 WebExtractor \ DataExtractor \ DataExtractorTypes;使用 WebExtractor \ Client \ Client;

$ factory = DataExtractorFactory :: getFactory(); $ extractor = $ factory-> createDataExtractor(DataExtractorTypes :: CSS); $ client = new 客戶端; $ content = $ client-> get('https://en.wikipedia.org/wiki/2014_Winter_Olympics'); $ extractor-> setContent($ content); $ h1 = $ extractor-> setSelector('h1') - > extract();