C# 采集程序

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using MSXML2;
using System.Text.RegularExpressions;
namespace wenweifeng
{
    
/// <summary>
    
/// CaiJi 的摘要说明
    
/// </summary>

    public class CaiJi
    
{
        
public CaiJi()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
~CaiJi()
        
{
            Dispose();
        }

        
public void Dispose()
        
{
         GC.SuppressFinalize(
this);
        }

  
     
日期随机函数

        
取得文件后缀

        
#region 获取远程文件源代码
        
/**/
        
/// <summary>
        
/// 获取远程文件源代码
        
/// </summary>
        
/// <param name="url">远程url</param>
        
/// <returns></returns>

        public string GetRemoteHtmlCode(string Url)
        
{
            
string s = "";
            MSXML2.XMLHTTP _xmlhttp 
= new MSXML2.XMLHTTPClass();
            _xmlhttp.open(
"GET", Url, falsenullnull);
            _xmlhttp.send(
"");
            
if (_xmlhttp.readyState == 4)
            
{
                s 
= System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
            }

            
return s;
        }


        
#endregion


        
#region 保存远程文件
        
/**********************************
         * 函数名称:RemoteSave
         * 功能说明:保存远程文件
         * 参    数:Url:远程url;Path:保存到的路径
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string s = "";
         *          string url = @"
http://www.baidu.com/img/logo.gif";
         *          string path =Server.MapPath("Html/");
         *          s = o.RemoteSave(url,path);
         *          Response.Write(s);
         *          o.Dispose();         
         * *****************************
*/

        
/// <summary>
        
/// 保存远程文件
        
/// </summary>
        
/// <param name="Url">远程url</param>
        
/// <param name="Path">保存到的路径</param>
        
/// <returns></returns>

        public string RemoteSave(string Url, string Path)
        
{
         
            Random ra 
= new Random();
            
string newfilename = string.Empty;
            
string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url);
            
if (GetFileExtends(Url).Trim().ToLower() != "jpg")
                newfilename 
= StringFileName;
            
else
                newfilename 
= DateRndName(ra) + "Addpic." + GetFileExtends(Url);
            
string StringFilePath = Path + StringFileName;
            
string newfilepath = Path + newfilename;
            
string retname = string.Empty;
            
try
            
{
                MSXML2.XMLHTTP _xmlhttp 
= new MSXML2.XMLHTTPClass();
                _xmlhttp.open(
"GET", Url, falsenullnull);
                _xmlhttp.send(
"");
                
if (_xmlhttp.readyState == 4)
                
{
                    
if (System.IO.File.Exists(StringFilePath))
                        System.IO.File.Delete(StringFilePath);
                    System.IO.FileStream fs 
= new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew);
                    System.IO.BinaryWriter w 
= new System.IO.BinaryWriter(fs);
                    w.Write((
byte[])_xmlhttp.responseBody);
                    w.Close();
                    fs.Close();
                    
if (GetFileExtends(Url).Trim().ToLower() == "jpg")
                    
{
                        function myfunction 
= new function();
                        
//myfunction.AddShuiYinWord(StringFilePath, newfilepath);
                        service myserver=new service();
                        myfunction.AddShuiYinPic(StringFilePath, newfilepath, HttpContext.Current.Server.MapPath(myserver.myweblogo));
                    }


                }

                
else
                    
throw new Exception(_xmlhttp.statusText);
            }

            
catch (Exception ex)
            
{

            }

            
return newfilename;
        }

        
#endregion


        
#region 替换网页中的换行和引号
        
/**********************************
         * 函数名称:ReplaceEnter
         * 功能说明:替换网页中的换行和引号
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"
http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.ReplaceEnter(HtmlCode);
         *          Response.Write(s);
         *          o.Dispose();
         * *******************************
*/

        
/// <summary>
        
/// 替换网页中的换行和引号
        
/// </summary>
        
/// <param name="HtmlCode">HTML源代码</param>
        
/// <returns></returns>

        public string ReplaceEnter(string HtmlCode)
        
{
            
string s = "";
            
if (HtmlCode == null || HtmlCode == "")
                s 
= "";
            
else
                s 
= HtmlCode.Replace("/"""");
            s = s.Replace("/r""");
            s 
= s.Replace("/n""");
            
return s;
        }


        
#endregion
               

        
#region 执行正则提取出值
        
/**********************************
         * 函数名称:GetRegValue
         * 功能说明:执行正则提取出值
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"
http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.ReplaceEnter(HtmlCode);
         *          string Reg="<title>.+?</title>";
         *          string GetValue=o.GetRegValue(Reg,HtmlCode)
         *          Response.Write(GetValue);
         *          o.Dispose();
         * *******************************
*/

        
/// <summary>
        
/// 执行正则提取出值
        
/// </summary>
        
/// <param name="RegexString">正则表达式</param>
        
/// <param name="RemoteStr">HtmlCode源代码</param>
        
/// <returns></returns>

        public string  GetRegValue(string RegexString, string RemoteStr)
        
{
            
string MatchVale = "";
            Regex r 
= new Regex(RegexString);
            Match m 
= r.Match(RemoteStr);
            
if (m.Success)
            
{
                MatchVale 
= m.Value;
            }

            
return MatchVale;
        }

        
#endregion
        

        
#region 替换HTML源代码
        
/**********************************
         * 函数名称:RemoveHTML
         * 功能说明:替换HTML源代码
         * 参    数:HtmlCode:html源代码
         * 调用示例:
         *          GetRemoteObj o = new GetRemoteObj();
         *          string Url = @"
http://www.baidu.com";
         *          string HtmlCode = o.GetRemoteHtmlCode(Url);
         *          string s = o.ReplaceEnter(HtmlCode);
         *          string Reg="<title>.+?</title>";
         *          string GetValue=o.GetRegValue(Reg,HtmlCode)
         *          Response.Write(GetValue);
         *          o.Dispose();
         * *******************************
*/

        
/// <summary>
        
/// 替换HTML源代码
        
/// </summary>
        
/// <param name="HtmlCode">html源代码</param>
        
/// <returns></returns>

        public string RemoveHTML(string HtmlCode)
        
{
            
string MatchVale = HtmlCode;          
            
foreach (Match s in Regex.Matches(HtmlCode, "<.+?>"))
            
{
                MatchVale 
= MatchVale.Replace(s.Value, "");
            }

            
return MatchVale;        
        }


        
#endregion


        
匹配页面的链接

        
匹配页面的图片地址

        
替换通过正则获取字符串所带的正则首尾匹配字符串


     


}


}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值