老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
_% N' I5 P" B9 L
s) l- u! L3 u: F5 m ~& u
网页采集程序(超级简单版)
$ t5 C/ I' G3 |- k9 L/ w$ P" ^
" ^, N; k, Q- h) |4 h

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

+ C* M6 v4 R" `6 Z0 X! R

02         { 

! M) j2 ~/ p4 L- y) J

03             //方法一: 

; a5 O' p. B6 U2 k2 Q0 H

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

7 G" x9 @, A3 ^& n% @0 D

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

) b1 a; c/ e; [2 y# m) L

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

# ]3 a/ k; b' y

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

, d- }; r# {$ N% D$ E. K8 z G0 a$ g

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

E5 o! L; |: s$ N! k) H. a

09             //Response.Write(html); 

; R9 v$ r8 t0 ^$ i

10   

1 P. b- K$ w1 m' z

11             //方法二: 

6 K. |% z, M6 p4 B7 s; K' c4 n/ v) ?( a

12         //获取整个网页 

7 A/ M- Q i$ O( [! ^ T

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

0 e3 a. N5 {( m. Y, ^. E

14             System.IO.Stream sm = wc.OpenRead("http://www.baidu.com"); 

8 S6 t0 v! a3 g7 S6 ]: e

15             System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000); 

: `8 }' e7 F, s( q

16             string html = sr.ReadToEnd(); 

6 x% ^3 ~1 f: A% K0 }

17             sr.Close(); 

1 i5 j7 j8 [2 K- O" e" W

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

7 `" Y$ u, ]: o. T* K" h

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

9 B8 {: j, r8 ~4 {

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

; Y" }9 ^6 G% B$ z& r e/ Q8 a

21             Response.Write(html); 

0 |: x9 p( _* a7 h. v2 e, [0 e* I

22         }


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

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






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