采集系统万能正则表达式

本文探讨了正则表达式在数据采集系统中的广泛应用,包括使用regex进行HTML内容提取、URL处理和解决null值问题。通过实例解析,展示了正则表达式的强大功能及其在处理复杂数据模式时的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

            
catch (Exception exception1)
            
{
            }

            
return text1;
        }


 public string SniffwebCode(string code, string wordsBegin, string wordsEnd)
        
{
            
string NewsTitle = "";
            Regex regex1 
= new Regex("" + wordsBegin + @"(?<title>[/s/S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            
for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
            
{
                NewsTitle 
= match1.Groups["title"].ToString();
            }

            
return NewsTitle;

        }

 

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

            
return urlList;

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值