[英]Extracting dom elements from html using PHP Simple HTML DOM Parser
我正在尝试使用PHP Simple HTML DOM PARSER从该站点提取包括文本的文章的链接。
我想提取主页上文章的所有h2
标签,并且我尝试通过这种方式进行操作:
$html = file_get_html('http://www.winbeta.org');
$articles = $html->getElementsByTagName('article');
$a = null;
foreach ($articles->find('h2') as $header) {
$a[] = $header;
}
print_r($a);
根据手册,应该首先获取article
标签中的所有内容,然后为每篇文章提取h2并保存在数组中。 但是相反,它给了我:
有几个问题:
getElementsByTagName
显然返回单个节点,而不是数组,因此,如果页面上有多个article标签,则它将不起作用。 而是使用find
来返回数组; find
的结果find
,所以你应该做的是对每一个人相匹配的商品标签,或更好的使用组合选择作为参数来find
; ->plaintext
显式检索节点的文本内容,否则将获得节点的对象表示以及其所有属性和内部信息; ’
。 这些可以使用html_entity_decode
解码。 所以这段代码应该可以工作:
$a = array();
foreach ($html->find('article h2') as $h2) { // any h2 within article
$a[] = html_entity_decode($h2->plaintext);
}
使用array_map
,您也可以这样:
$a = array_map(function ($h2) { return html_entity_decode($h2->plaintext); },
$html->find('article h2'));
如果还需要检索文章中的其他标签,并将其文本存储在不同的数组中,则可以执行以下操作:
$a = array();
$b = array();
foreach ($html->find('article') as $article) {
foreach ($article->find('h2') as $h2) {
$a[] = html_entity_decode($h2->plaintext);
}
foreach ($article->find('h3') as $h3) {
$b[] = html_entity_decode($h3->plaintext);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.