正则表达式获取远程网页

[一篮饭特稀原创,转载请注明出处http://www.cnblogs.com/wanghafan/archive/2012/02/09/2344276.html]

--简单例子: 

View Code
1 using System.Text;
2 using System.Text.RegularExpressions;
3
4 string str = "<img>123456789</img>";
5 string regexStr = @"<img>([^<]*)</img>";
6 Match mc = Regex.Match(str, regexStr, RegexOptions.IgnoreCase);
7 Response.Write(mc.Groups[1].Value);

--天气例子: 

View Code
 1 using System.Text;
2 using System.Text.RegularExpressions;
3
4 string str = GetRequestString("http://www.ntxnw.com.cn/tq.asp?s_type=00", 8000, 1, Encoding.GetEncoding("GB2312"));
5 string regexStr = XXXXXXXXXXXXXXXXXXXXXX;
6 Match mc = Regex.Match(str, regexStr, RegexOptions.IgnoreCase);
7 this.divContent.InnerHtml=mc.Groups[1].Value;
8 /// <summary>
9 /// 获取指定远程网页内容
10 /// </summary>
11 /// <param name="strUrl">所要查找的远程网页地址</param>
12 /// <param name="timeout">超时时长设置,一般设置为8000</param>
13 /// <param name="enterType">是否输出换行符,0不输出,1输出文本框换行</param>
14 /// <param name="EnCodeType">编码方式</param>
15 /// <returns></returns>
16 /// 也可考虑 static string
17 public string GetRequestString(string strUrl, int timeout, int enterType, System.Text.Encoding EnCodeType)
18 {
19 string strResult;
20 try
21 {
22 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
23 myReq.Timeout = timeout;
24 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
25 Stream myStream = HttpWResp.GetResponseStream();
26 StreamReader sr = new StreamReader(myStream, EnCodeType);
27 StringBuilder strBuilder = new StringBuilder();
28 while (-1 != sr.Peek())
29 {
30 strBuilder.Append(sr.ReadLine());
31 if (enterType == 1)
32 {
33 strBuilder.Append("\r\n");
34 }
35 }
36 strResult = strBuilder.ToString();
37 }
38 catch (Exception err)
39 {
40 strResult = "请求错误:" + err.Message;
41 }
42 return strResult;
43 }




原文地址:https://www.cnblogs.com/wanghafan/p/2344276.html