老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
3 H. o) P, ?% @* C
' T& `3 A, S" W' `" l
网页采集程序(超级简单版)
3 F. S, Z+ Q2 B9 ^. F0 G
! I8 |, `* Q$ O( q, Q

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

. x/ d# X& m( t) }* N

02         { 

# Y$ ^2 I3 S/ ?% @4 C8 D

03             //方法一: 

1 \9 R* i7 v, H# q: |& R. F

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

7 Q' Z7 L9 x% p- n/ v/ ^

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

0 [& O/ G- w! m: q

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

$ Z% L( I3 r. F5 \

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

. f( g* R M" r. V- f

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

: x2 F) C8 g7 `# I; Y

09             //Response.Write(html); 

# a8 p1 K: \* V' Y

10   

8 P z" z3 I" I5 D+ X) \; }5 M

11             //方法二: 

e$ g) \* z9 ]( P9 T: y! c" k

12         //获取整个网页 

/ k% K p- l4 g. I

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

# `. t" n5 s$ T. [, |( l+ ]

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

0 F+ @1 |5 N/ k# G

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

' Z, r9 }8 i: f1 {8 @- n( x$ ~7 y7 w) n

16             string html = sr.ReadToEnd(); 

2 j7 M4 @5 X) J/ S6 D7 Q8 B

17             sr.Close(); 

1 u6 g7 t* E ?; U: h$ G

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

& X& h0 y1 b B" n% J

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

[3 h0 Q4 R0 [0 M% K

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

# B7 u7 O" S. c- ^

21             Response.Write(html); 

- [% q1 o2 g1 |' H$ u! Q9 s* ^

22         }


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

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






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