老秘网_材夜思范文

标题: 网页采集程序(超级简单版) [打印本页]

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
2 ]. g; d: U+ A; k% t- v
; m% E& U G3 E! f& g7 a7 C2 T
网页采集程序(超级简单版)
/ ~( U, @/ ?5 J* V8 Y& k% }8 Z1 g9 @' b# H
9 W: i; ]4 | l, r: |: K% ^9 }

网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e) 

$ ?0 J$ G5 Q$ N/ d- U) u

02         { 

2 l" _. c* s+ c/ |- r

03             //方法一: 

1 O# ^" u- ~. m: x9 h- u

04             //System.Net.WebClient wc = new System.Net.WebClient(); 

4 k5 l7 ^ P0 S, Q O5 f

05             //byte[] b = wc.DownloadData("http://www.baidu.com"); 

$ U q! x5 Z9 \; s8 X2 j* t

06             //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b); 

# \; y3 L; e) M0 ~0 C9 d

07             //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length); 

0 V: o* T. V4 ?( v; [1 r

08             //html = html.Substring(0, html.IndexOf("</p>")); 

# j- T" |7 d3 y. `% L4 t8 Q: ], O

09             //Response.Write(html); 

5 X) j' ?) X5 F- V: M7 }) g! L8 ]

10   

! J$ f0 Q4 m. ]8 A7 c5 J

11             //方法二: 

6 _4 H5 K7 `% f6 {( s' `

12         //获取整个网页 

! u& R6 c. x0 F& H# l5 m

13             System.Net.WebClient wc = new System.Net.WebClient(); 

5 L9 W$ J% f/ @# r5 [: v6 f0 G7 c

14             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 O

16             string html = sr.ReadToEnd(); 

4 \" h1 O% d3 x4 R* W' P

17             sr.Close(); 

* L" F) ?- L- X

18             //根据规则获取想要的内容 

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+ c

20             html = html.Substring(0, html.IndexOf("</p>")); 

; }5 u1 J: x' y1 c1 _: Y) G+ J3 h

21             Response.Write(html); 

7 E+ D( f+ _+ j- t4 ]( F# {

22         }


作者: 福建老秘    时间: 2010-7-20 20:00

http://hereson.javaeye.com/blog/207468






欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) Powered by Discuz! X3.4