老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
^+ V I) X( D/ t: V% e6 U, H9 n5 S
0 h3 S' q! C7 |: z! @
网页采集程序(超级简单版)
K( d, o( M c( v
& c. f+ ]3 U- {8 _4 Y9 Q0 ?* c

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

9 W z, x7 r- d# }( F/ q8 }0 N

02         { 

* N9 Y9 u! x. n7 w

03             //方法一: 

" {. e' @0 b4 E; E

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

8 z1 S1 p. ^- @" [

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

+ o. \. J8 X/ q; I5 L5 K

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

0 }; m. ~" |1 |$ z9 z7 b, g- l

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

9 d) N# D, y- F: e( j

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

/ ~# I& N. M! _, b. h) |

09             //Response.Write(html); 

1 Q7 f l( P% r% E; L) p

10   

0 m9 U9 j# L; w5 ]

11             //方法二: 

2 s3 g4 U2 K' [& A# H

12         //获取整个网页 

& z7 y. r% x+ F0 ?7 \# j, y4 d) v

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

% q; J% ^8 k( r

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

* \) J6 O8 U% b4 Z

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

" \( V' O V" K3 E

16             string html = sr.ReadToEnd(); 

3 O! [! o! a; Z1 a E* K2 \

17             sr.Close(); 

+ T$ r, a# A( Q! }2 f9 ?& H+ ^# O

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

1 b, v, K4 n$ P% [" Z0 p$ H+ H

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

; P! {+ j3 Y( R: u; @' ^

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

2 s) l' m6 `, N. F6 b+ ^; [

21             Response.Write(html); 

& `. b+ ] C( s

22         }


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

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






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