网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
2 l" _. c* s+ c/ |- r03 //方法一:
1 O# ^" u- ~. m: x9 h- u04 //System.Net.WebClient wc = new System.Net.WebClient();
4 k5 l7 ^ P0 S, Q O5 f05 //byte[] b = wc.DownloadData("http://www.baidu.com");
$ U q! x5 Z9 \; s8 X2 j* t06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
# \; y3 L; e) M0 ~0 C9 d07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
0 V: o* T. V4 ?( v; [1 r08 //html = html.Substring(0, html.IndexOf("</p>"));
# j- T" |7 d3 y. `% L4 t8 Q: ], O09 //Response.Write(html);
5 X) j' ?) X5 F- V: M7 }) g! L8 ]10
! J$ f0 Q4 m. ]8 A7 c5 J11 //方法二:
6 _4 H5 K7 `% f6 {( s' `12 //获取整个网页
! u& R6 c. x0 F& H# l5 m13 System.Net.WebClient wc = new System.Net.WebClient();
5 L9 W$ J% f/ @# r5 [: v6 f0 G7 c14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
+ F) i- @/ D0 b# T; d* r' t5 Z( [15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
; z! r& m( z3 c( T- f" ?+ f& p: u0 O16 string html = sr.ReadToEnd();
4 \" h1 O% d3 x4 R* W' P17 sr.Close();
* L" F) ?- L- X18 //根据规则获取想要的内容
1 f. J0 }2 q/ a4 p1 M" K# ]19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
) i2 \/ F1 \ c1 x+ c20 html = html.Substring(0, html.IndexOf("</p>"));
; }5 u1 J: x' y1 c1 _: Y) G+ J3 h21 Response.Write(html);
7 E+ D( f+ _+ j- t4 ]( F# {22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) | Powered by Discuz! X3.4 |