老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
. J0 W0 H0 g: P. \( I
" v7 n# O) A) s- Z5 h9 L i: q% _
网页采集程序(超级简单版)
- h' |1 T7 ?& L- Z
2 _8 s6 a y2 G0 z! p

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

' X+ T `0 V9 z

02         { 

. G, m' |* k$ p( Y# \& r

03             //方法一: 

9 J3 D5 |3 b" _! ?7 {- V9 [7 |

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

8 Q3 W7 V' f$ l* A* \$ n

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

0 U/ l1 r( Y2 Q6 X' q5 P9 P" N

06             //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$ N

08             //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 J

12         //获取整个网页 

1 H+ d. T9 e9 ~1 z, q+ D

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

( Q/ T& k9 q. n4 C1 ]! c8 a/ E

14             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* h

16             string html = sr.ReadToEnd(); 

1 e f. F, ~7 I. u8 O; [1 Q

17             sr.Close(); 

! M- `; J# f1 t/ t( J) J9 ^+ @8 s

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

. m. {& ^8 D1 R: s# K O+ s

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

6 l% E0 k( l. S% }6 ]: G

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

' B! i' ^: L' }5 I

21             Response.Write(html); 

z. k$ ]3 y" T' H# ^

22         }


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

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






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