老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
7 f! \* n M1 ?& _( e# X _
3 }" W# o; z( P8 D% K+ y
网页采集程序(超级简单版)
0 ]) Y3 F8 B# ]* d! I
' P9 {+ v# _4 l) ~" G) D& f# y4 S

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

. i" ^- g" W( l' I5 D3 M& L& [

02         { 

0 `$ r! D7 J$ T; h; A

03             //方法一: 

: @" G7 o8 h: X B: e( x

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

! l% @1 D+ U9 J& l

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

# b" E0 l5 I# u+ W8 W" [

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

- ?/ q6 i& M* m

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

" Y" {! P& E, V9 E2 x# P

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

3 S* _. z! |! E6 Q. @+ m9 N! I/ N

09             //Response.Write(html); 

4 V4 Q) l" x% @

10   

. Q$ N& I/ g* a" E! V, @: ]7 V

11             //方法二: 

( W( h. f ]" h$ E- Y

12         //获取整个网页 

; @. b+ L/ S6 [9 [

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

2 l: F8 }& R7 H8 n/ A' Z

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

6 I/ |8 d9 y! V. w, c! Q& G

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

) w* U; e3 _9 ?- v

16             string html = sr.ReadToEnd(); 

8 @. T; S/ X0 w0 ?

17             sr.Close(); 

7 e" m9 o3 D3 v/ F7 H2 f/ Y+ d

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

7 O& b' z1 C6 s+ A6 M4 h

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

) r* l4 s6 X/ q8 G/ }& w

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

9 }( a& D2 n: C0 D

21             Response.Write(html); 

c9 j `( T6 }' V1 G1 G) ~

22         }


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

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






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