网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
! M) j2 ~/ p4 L- y) J03 //方法一:
; a5 O' p. B6 U2 k2 Q0 H04 //System.Net.WebClient wc = new System.Net.WebClient();
7 G" x9 @, A3 ^& n% @0 D05 //byte[] b = wc.DownloadData("http://www.baidu.com");
) b1 a; c/ e; [2 y# m) L06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
# ]3 a/ k; b' y07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
, d- }; r# {$ N% D$ E. K8 z G0 a$ g08 //html = html.Substring(0, html.IndexOf("</p>"));
E5 o! L; |: s$ N! k) H. a09 //Response.Write(html);
; R9 v$ r8 t0 ^$ i10
1 P. b- K$ w1 m' z11 //方法二:
6 K. |% z, M6 p4 B7 s; K' c4 n/ v) ?( a12 //获取整个网页
7 A/ M- Q i$ O( [! ^ T13 System.Net.WebClient wc = new System.Net.WebClient();
0 e3 a. N5 {( m. Y, ^. E14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
8 S6 t0 v! a3 g7 S6 ]: e15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
: `8 }' e7 F, s( q16 string html = sr.ReadToEnd();
6 x% ^3 ~1 f: A% K0 }17 sr.Close();
1 i5 j7 j8 [2 K- O" e" W18 //根据规则获取想要的内容
7 `" Y$ u, ]: o. T* K" h19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
9 B8 {: j, r8 ~4 {20 html = html.Substring(0, html.IndexOf("</p>"));
; Y" }9 ^6 G% B$ z& r e/ Q8 a21 Response.Write(html);
0 |: x9 p( _* a7 h. v2 e, [0 e* I22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (https://www.laomiw.com/) | Powered by Discuz! X3.4 |