如何在不丢失标签的情况下抓取网页数据 [英] how to scrape web page data without losing tags

查看:127
本文介绍了如何在不丢失标签的情况下抓取网页数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php和dom xpath来擦除网页数据。当我将$ node-> nodeValue存储到我的数据库中,或者即使我尝试回显它,所有标签如< p> ; br> 缺少。所以我把所有的段落连接起来。如何解决这个问题

解决方案

如果你有一个节点,你需要所有的内容,你可以使用这个功能:

  function innerHTML(DOMNode $ node)
{
$ doc = new DOMDocument();
foreach($ node-> childNodes as $ child){
$ doc-> appendChild($ doc-> importNode($ child,true));
}
return $ doc-> saveHTML();
}


I am trying to scrape web data using php and dom xpath. When I store the $node->nodeValue into my database or even if i try to echo it, all the tags like <p> and <br> are missing. So I am getting all the paras concatenated. How to solve this problem

解决方案

If you have a node, and you need all its contents as they are, you can use this function:

function innerHTML(DOMNode $node)
{
  $doc = new DOMDocument();
  foreach ($node->childNodes as $child) {
    $doc->appendChild($doc->importNode($child, true));
  }
  return $doc->saveHTML();
}

这篇关于如何在不丢失标签的情况下抓取网页数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆