我正在尝试使用DOMDocument来解析HTML代码.
我只是在做一些简单的工作,我已经在scrap data using regex and simplehtmldom上喜欢gordon的答案,并根据他的工作编写了我的代码.
我发现PHP.net上的文档不是很好,因为信息有限,几乎没有示例,大多数细节都是基于解析XML.
$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));
?>
出于测试目的,我试图获取nu.nl新闻文章的标题(在h1标签之间)和内容(HTML).
正如你所看到的,我可以得到标题,虽然我对那个评估字符串甚至不满意,因为它恰好是该div级别上唯一的h1标签.
解决方法:
以下是使用DOM和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)
);
XPath字符串(id(“leadarticle”)/ div / h1)将返回h1的textContent,它是div的子节点,div是具有id leadarticle的元素的子节点.
XPath id(“leadarticle”)/ div [@ class =“content”]将返回具有class属性内容的div,该属性内容是具有id leadarticle的元素的子元素.
标签:php,dom,parsing,xpath
来源: https://codeday.me/bug/20190929/1833095.html