正規表現リモートWebページの取得

8506 ワード

[一篮饭特稀オリジナル,转载请注明出典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
");
34 }
35 }
36 strResult = strBuilder.ToString();
37 }
38 catch (Exception err)
39 {
40 strResult = "" + err.Message;
41 }
42 return strResult;
43 }