none
IHTMLDOMNode结点删除后,网页中却还在显示,怎么回事呢? RRS feed

  • 问题

  • 环境:VS2005 C# WinForm IE8

    当网页加载完后,我遍历网页的DOM树,找到position属性为fixed的div节点。如果该节点下还包括img、object或iframe节点,就删除掉div节点。(过滤广告)

    通过过滤前后比较dom树,广告div节点确实被删去了。但网页中却依然在显示,为什么呢?

    注:这种情况仅存在于个别网页。

    详细代码在这里:

        private void button2_Click(object sender, EventArgs e)
        {
          IHTMLDocument3 HTMLDocument = (IHTMLDocument3)this.webBrowser1.Document.DomDocument;
          IHTMLDOMNode rootDomNode = (IHTMLDOMNode)HTMLDocument.documentElement;
          IHTMLDOMNode bodyNode = null;
    
          if (rootDomNode.hasChildNodes())
          {
            IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)rootDomNode.childNodes;
            int length = allchild.length;
            for (int i = 0; i < length; i++)
            {
              IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
              if (string.Equals(child_node.nodeName,"body",StringComparison.OrdinalIgnoreCase))
              {
                bodyNode = child_node;
                break;
              }
            }
          }
    
          ArrayList del = new ArrayList();
    
          if (bodyNode != null && bodyNode.hasChildNodes())
          {
            IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)bodyNode.childNodes;
            int length = allchild.length;
            for (int i = 0; i < length; i++)
            {
              IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
              if (string.Equals(child_node.nodeName, "div", StringComparison.OrdinalIgnoreCase))
              {
                IHTMLDOMNode fix = GetFixedDiv(child_node);
    
                if(fix != null)
                {
                  if (NeedFilter(fix))
                  { del.Add(fix); }
                }
              }
            }
    
            for (int i = 0; i < del.Count;++i )
            {
              IHTMLDOMNode node = (IHTMLDOMNode)del[i];
              ((IHTMLElement)node).innerHTML = "";
              node.removeNode(true);
            }
          }
        }
    
        private IHTMLDOMNode GetFixedDiv(IHTMLDOMNode div)
        {
          if(div != null)
          {
            IHTMLStyle style = ((IHTMLElement)div).style;
            if (string.Equals("fixed", style.position, StringComparison.OrdinalIgnoreCase))
            {
              return div;
            }
    
            if (div.hasChildNodes())
            {
              IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)div.childNodes;
              int length = allchild.length;
    
              for (int i = 0; i < length; i++)
              {
                IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
                if (string.Equals(child_node.nodeName, "div", StringComparison.OrdinalIgnoreCase))
                {
                  IHTMLDOMNode re = GetFixedDiv(child_node);
                  if(re != null)
                  { return re; }
                }
              }
            }
          }
          return null;
        }
    
        private bool NeedFilter(IHTMLDOMNode node)
        {
          if (node != null && node.hasChildNodes())
          {
            //this.textBox1.Text += "\n\n";
    
            IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)node.childNodes;
            int length = allchild.length;
            for (int i = 0; i < length; i++)
            {
              IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i);
              //this.textBox1.Text += (child_node.nodeName + "\n");
    
              if (string.Equals(child_node.nodeName, "img", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(child_node.nodeName, "object", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(child_node.nodeName, "iframe", StringComparison.OrdinalIgnoreCase))
              {
                return true;
              }
    
              if (NeedFilter(child_node))
              { return true; }
            }
          }
          return false; 
        }
    


    2011年8月11日 15:02

答案

全部回复