Html代码白名单过滤插件(C#)

作者:小文 发布于:2010-10-18 9:28 Monday 分类:免费插件

该插件将过滤掉所有除需要外的html代码.在处理前您需要使用采集器过滤js,css等代码.您可以下载该插件或是自己修改代码以达到自己的需要.

插件主要源码:

using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

/*注意事项
 * 1.这个命名空间不能更改.当然,你可以在其它的文件里使用其它的命名空间,在这里使用.
 * 2.必须引用采集器目录下的LeWell.dll文件.
 * 3.必须实现 IPlugin接口 里的 Run 方法
 * 4.编写过程出请对传入传出的参数进行检测
 * */

namespace LeWell.Plugins
{
    public class Demo : LeWell.Plugins.IPlugin   //使用的IPlugin接口,请引用 LeWell.Plugins.dll 文件
    {
        /// <summary>
        /// 插件运行处理预留方法
        /// </summary>
        /// <param name="str">要处理的源代码</param>
        /// <param name="pageurl">采集页面地址</param>
        /// <param name="pagetype">页面类型为一枚举 LeWell.Plugins.PageType ,其中List,Pages,Content,Save分别代表列表页,分页或多页,默认页,保存时</param>
        /// <param name="encoding">页面编码</param>
        /// <param name="cookies">网站的cookies</param>
        /// <returns></returns>
        public string Run(string str, string pageurl, LeWell.Plugins.PageType pagetype, Encoding encoding, System.Net.CookieCollection cookies)
        {
            ////请在这里执行您的操作,返回string
            //string result = "这个结果是单页面测试,后边的数据为程序接收到的数据:\r\n\r\n";
            //result += "当前的页面网址为:" + pageurl.ToString() + "\r\n";
            //result += "当前的页面类型为:" + pagetype.ToString() + "\r\n";
            //result += "当前的网页编码为:" + encoding.ToString() + "\r\n";
            //result += "当前网页的代码为:" + str.ToString() + "\r\n";
            return str;
        }

      
        /// <summary>
        /// 这个是用来在最后处理采集器入库前的数据的
        /// </summary>
        /// <param name="ht"></param>
        /// <param name="pageurl"></param>
        /// <param name="encoding"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public Hashtable Run(Hashtable ht, string pageurl, Encoding encoding, System.Net.CookieCollection cookies)
        {
            System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
            if (ht.ContainsKey("内容"))
            {
                string data = ht["内容"].ToString();
                if (string.IsNullOrEmpty(data)) return ht;

                list.Add("div");
                list.Add("ul");
                list.Add("dt");
                list.Add("dl");
                list.Add("dd");
                list.Add("table");
                list.Add("tbody");
                list.Add("tr");
                list.Add("td");
                list.Add("p");

                foreach (string s in list)
                {
                    data = System.Text.RegularExpressions.Regex.Replace(data, "<(/?)" + s + "[^>]*?>", "<$1" + s + ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    data = System.Text.RegularExpressions.Regex.Replace(data, "<(/?)" + s + ">", "ASDFGHJKL$1" + s, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                }

                data = System.Text.RegularExpressions.Regex.Replace(data, "<br(\\s)?/?>", "QWERTYUIOPB", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                List<KeyValuePair<string, string>> dic = new List<KeyValuePair<string, string>>();
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("<a[^>]*?>[^<]*?</a>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.MatchCollection mc = regex.Matches(data);
                foreach (System.Text.RegularExpressions.Match m in mc)
                {
                    dic.Add(new KeyValuePair<string, string>(m.Value, base64_encode(m.Value)));
                }

                regex = new System.Text.RegularExpressions.Regex("<img\\s+[^>]*?>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                mc = regex.Matches(data);
                foreach (System.Text.RegularExpressions.Match m in mc)
                {
                    dic.Add(new KeyValuePair<string, string>(m.Value, base64_encode(m.Value)));
                }

                data = System.Text.RegularExpressions.Regex.Replace(data,"<[^>]*?>", "");

                foreach (KeyValuePair<string, string> kv in dic)
                {
                    data = data.Replace(kv.Value, kv.Key);
                }

                foreach (string s in list)
                {
                    data = data.Replace("ASDFGHJKL" + s, "<" + s + ">");
                    data = data.Replace("ASDFGHJKL/" + s, "</" + s + ">");
               }
                data = data.Replace("QWERTYUIOPB","<br>");
                ht["内容"] = data;
            }
            return ht;
        }

        /// <summary>
        ///Base64加密
        /// </summary>
        /// <returns>返回string</returns>
        public  string base64_encode(string s)
        {
            string strResult = "";

            if ((s != null) && (s != ""))
            {
                strResult = Convert.ToBase64String(System.Text.ASCIIEncoding.Default.GetBytes(s));
            }

            return strResult;
        }

        /// <summary>
        ///Base64解密
        /// </summary>
        /// <returns>返回string</returns>
        public  string base64_decode(string s)
        {
            string strResult = "";

            if ((s != null) && (s != ""))
            {
                int buling = s.Length % 4;
                if (buling == 3) s = s + "=";
                else if (buling == 2) s = s + "==";
                else if (buling == 1) s = s.Substring(0, s.Length - 1);
                strResult = System.Text.ASCIIEncoding.Default.GetString(Convert.FromBase64String(s));
            }

            return strResult;
        }
    }

}
 

标签: 过滤

评论(0) 引用(0) 浏览(9117)

Powered by emlog