网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
. G, m' |* k$ p( Y# \& r03 //方法一:
9 J3 D5 |3 b" _! ?7 {- V9 [7 |04 //System.Net.WebClient wc = new System.Net.WebClient();
8 Q3 W7 V' f$ l* A* \$ n05 //byte[] b = wc.DownloadData("http://www.baidu.com");
0 U/ l1 r( Y2 Q6 X' q5 P9 P" N06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
1 y! r/ u p8 k: a( {07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
) H6 D3 D, o. y9 `5 o$ N08 //html = html.Substring(0, html.IndexOf("</p>"));
) M3 b9 @$ z1 {; _" ]- F$ H- h- [09 //Response.Write(html);
2 D2 h5 U" X2 `10
/ m- v2 f, ^# g$ _7 _' l9 ]11 //方法二:
" c! B) m2 f' m* w, w" }3 h J12 //获取整个网页
1 H+ d. T9 e9 ~1 z, q+ D13 System.Net.WebClient wc = new System.Net.WebClient();
( Q/ T& k9 q. n4 C1 ]! c8 a/ E14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
) t f; G9 @+ ~" Q6 j3 \ A( ~15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
" L% P5 y1 O) Q- s9 i$ a/ U* h16 string html = sr.ReadToEnd();
1 e f. F, ~7 I. u8 O; [1 Q17 sr.Close();
! M- `; J# f1 t/ t( J) J9 ^+ @8 s18 //根据规则获取想要的内容
. m. {& ^8 D1 R: s# K O+ s19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
6 l% E0 k( l. S% }6 ]: G20 html = html.Substring(0, html.IndexOf("</p>"));
' B! i' ^: L' }5 I21 Response.Write(html);
z. k$ ]3 y" T' H# ^22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) | Powered by Discuz! X3.4 |