采集系统万能正则表达式

由于经常要写一些采集的程序,下面的三个函数是采集中的很常用的函数。姑且叫采集系统万能正则表达式吧。全部源码见
http://www.softbk.com/news.asp?id=3564 <script type="text/javascript"><!-- google_ad_client = "pub-4334685396432654"; //468x15, 创建于 07-12-5 google_ad_slot = "3722935453"; google_ad_width = 468; google_ad_height = 15; //--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>     欢迎一起交流

None.gif // 获取页面的html源码
None.gif
  public    string  GetHtmlSource( string  Url,  string  charset)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (charset == "" || charset == null) charset = "gb2312";
InBlock.gif            
string text1 = "";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HttpWebRequest request1 
= (HttpWebRequest)WebRequest.Create(Url);
InBlock.gif                HttpWebResponse response1 
= (HttpWebResponse)request1.GetResponse();
InBlock.gif                Stream stream1 
= response1.GetResponseStream();
InBlock.gif                StreamReader reader1 
= new StreamReader(stream1, Encoding.GetEncoding(charset));
InBlock.gif                text1 
= reader1.ReadToEnd();
InBlock.gif                stream1.Close();
InBlock.gif                response1.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exception1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return text1;
ExpandedBlockEnd.gif        }


None.gif   public   string  SniffwebCode( string  code,  string  wordsBegin,  string  wordsEnd)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string NewsTitle = "";
InBlock.gif            Regex regex1 
= new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
InBlock.gif            
for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NewsTitle 
= match1.Groups["title"].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return NewsTitle;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif   public  ArrayList SniffwebCodeReturnList( string  code,  string  wordsBegin,  string  wordsEnd)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            ArrayList urlList 
= new ArrayList();
InBlock.gif            
//string NewsTitle = "";
InBlock.gif
            Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
InBlock.gif            
for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                urlList.Add(match1.Groups[
"title"].ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return urlList;
InBlock.gif
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值