如何使用dom php解析器

时间:2009-06-06 23:55:17

标签: php dom html-parsing

我是PHP的DOM解析新手:
我有一个我正在尝试解析的HTML文件。它有一堆像这样的DIV:

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

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

我正在尝试使用php获取许多div框的内容。 如何使用DOM解析器执行此操作?

谢谢!

4 个答案:

答案 0 :(得分:20)

首先我必须告诉你,你不能在两个不同的div上使用相同的id;有关于这一点的课程。每个元素都应该有唯一的id。

使用id =“interestingbox”获取div内容的代码

$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页面。

答案 1 :(得分:6)

我使用simplehtmldom作为开头让这个工作:

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

答案 2 :(得分: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 />"  

答案 3 :(得分:0)

WebExtractor:https://github.com/knyga/webextractor 它可以用css,regex,xpath选择器解析页面。

查看包和测试示例:

  

使用WebExtractor \ DataExtractor \ DataExtractorFactory;使用   WebExtractor \ DataExtractor \ DataExtractorTypes;使用   WebExtractor \客户\客户端;

     

$ factory = DataExtractorFactory :: getFactory(); $ extractor =   $出厂&GT; createDataExtractor(DataExtractorTypes :: CSS); $ client = new   客户; $ content =   $客户端 - &GT;获得( 'https://en.wikipedia.org/wiki/2014_Winter_Olympics');   $ extractor-&GT; setContent($内容); $ h1 =   $ extractor-&GT; setSelector( 'H1') - &gt;提取();