老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
% E, ?) l W: I, |6 ~& A+ M) W
7 {# N _9 M3 }% F3 V1 e" O& c5 |
网页采集程序(超级简单版)
- @' a# x U& r+ A6 I( T5 Z
) a* H- ~( e8 @) j2 b9 [

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

: d% q6 S. R% E4 a

02         { 

- s% L. Z) r1 @/ F8 R/ ?7 Y

03             //方法一: 

) z& D+ H k7 U7 E

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

3 Y/ K3 y7 F, E" q5 G5 u

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

1 f6 m% l4 r- P6 v7 j+ ~

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

& R: z) k) L Y/ A) b$ L

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

P) R$ C( a) r1 N

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

8 ^% b6 s6 ] D( e

09             //Response.Write(html); 

[+ q5 q4 `% L- l+ N0 s- i' C

10   

. @( t9 B, ^9 @/ w: A' ]/ t

11             //方法二: 

+ n1 }! i f" H3 T q0 G

12         //获取整个网页 

: E5 W8 J/ }% K6 H% p/ r' D

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

9 Z8 L+ d. F3 `' e. |0 u' B

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

. @: B. {5 c& {3 P

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

% G) N3 h9 Q4 j1 y4 g$ ~! v2 p. M

16             string html = sr.ReadToEnd(); 

8 c. p, Z5 a9 z3 {* ]/ Y

17             sr.Close(); 

" d" h! f' Z+ K. g% @

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

1 s/ f/ l1 `; d5 q2 h% y5 g, I

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

% N0 h; G3 ?" K+ z S

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

4 S* |& {: W1 s6 c: C2 N

21             Response.Write(html); 

6 \; { L: F" D6 f$ }2 T

22         }


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

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






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