DOMDocument for parsing HTML (instead of regex)

2019-01-20 14:25发布

I am trying to learn using DOMDocument for parsing HTML code.

I am just doing some simple work, I already liked gordon's answer on scrap data using regex and simplehtmldom and based my code on his work.

I found documentation on PHP.net not that good due to limited information, almost no examples, and most specifics were based on parsing XML.

<?php
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www.nu.nl/internet/1106541/taalunie-keurt-open-sourcewoordenlijst-goed.html');
libxml_clear_errors();

$recipe = array();
$xpath = new DOMXPath($dom);
$contentDiv = $dom->getElementById('page'); // would have preferred getContentbyClass('content') (unique) in this case.

# title
print_r($xpath->evaluate('string(div/div/div/div/div/h1)', $contentDiv));

# content (this is not working)
#print_r($xpath->evaluate('string(div/div/div/div['content'])', $contentDiv)); // if only this worked
print_r($xpath->evaluate('string(div/div/div/div)', $contentDiv));
?>

For testing purposes I am trying to get the title (between h1 tags) and content (HTML) of a nu.nl news article.

As you can see I can get the title, although I am not even that happy with that evaluate string since it just happens to be the only h1 tag on that div-level.

2条回答
来,给爷笑一个
2楼-- · 2019-01-20 14:30

You shouldn't bother with the raw DOMDocument interface. Rather use one of the jQuery-style classes for extraction. How to parse HTML with PHP?

QueryPath seems to work fine if you use more specific selectors:

include "qp.phar";
$qp = htmlqp("http://www.nu.nl/internet/1106541/taalunie-keurt-open-sourcewoordenlijst-goed.html");

print $qp->find(".header h1")->text();
print $qp->top()->find(".article .content")->xhtml();

You might need to strip the intermingled Javascript before however (->find("script")->remove()).

查看更多
Bombasti
3楼-- · 2019-01-20 14:52

Here is how you could do it with DOM and XPath:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www.nu.nl/…');
libxml_clear_errors();

$xpath = new DOMXPath($dom);
echo $xpath->evaluate('string(id("leadarticle")/div/h1)');
echo $dom->saveHtml(
    $xpath->evaluate('id("leadarticle")/div[@class="content"]')->item(0)
);

The XPath string(id("leadarticle")/div/h1) will return the textContent of the h1 that is a child of a div that is the child of the element with the id leadarticle.

The XPath id("leadarticle")/div[@class="content"] will return the div with the class attribute content that is a child of the element with the id leadarticle.

Because you want the outerHTML of the content div you'll have to fetch the entire node and not just the content, hence no string() function in the XPath. Passing a node to the DOMDocument::saveHTML() method (which is only possible as of 5.3.6) will then serialize that node back to HTML.

查看更多
登录 后发表回答