老秘网_材夜思范文

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

作者: 福建老秘    时间: 2010-7-20 19:53
标题: 网页采集程序(超级简单版)
+ D3 G+ T" A( U4 ^1 W4 b4 q
( ^% w2 r6 p1 L, @& }! D) a$ _" h! C
网页采集程序(超级简单版)
' E# z! J+ f, ~6 ~( V9 w/ q u0 q
4 C1 u: F, X" M8 y0 I6 D

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

7 y8 d9 C! ~, t2 G! W3 G* v

02         { 

4 e+ }4 G! X4 w, P

03             //方法一: 

8 ]: ?) K' m1 W$ ~( q1 _% Z+ W

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

' ?. N3 ] ~* G# ?

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

5 }% Q) Z( l) w9 E: \; q2 s

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

6 R0 m) ]5 ~; |$ N% k4 Y/ C

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

( _6 f0 t2 d- y0 ^7 r* h

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

s5 n& Z5 J8 [8 e4 S

09             //Response.Write(html); 

# @) d3 H3 [+ ^- M6 K! l

10   

2 y. a) g- D `0 T

11             //方法二: 

# M0 j# J* ~0 x; ~

12         //获取整个网页 

8 J% M# c1 a. H& c' r: B0 c+ V

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

- K/ e, k. w5 n; g. h! w

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

+ } ~( S, i: c# e9 Y; Q) R

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

4 m" t- ]# b+ U! M4 S, Z

16             string html = sr.ReadToEnd(); 

1 Y- J! B- y) }. M' V$ N( X; h6 e( B

17             sr.Close(); 

3 d' {2 J# Y# S3 e" u$ s5 m

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

0 c+ P( m0 a' H) H- e

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

/ q+ [" d( e) I+ n3 S+ V0 `4 N: g. t

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

& ~, j y% f4 O

21             Response.Write(html); 

% \! s0 U) N0 i% D1 x2 c0 E7 i

22         }


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

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






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