网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
* N9 Y9 u! x. n7 w03 //方法一:
" {. e' @0 b4 E; E04 //System.Net.WebClient wc = new System.Net.WebClient();
8 z1 S1 p. ^- @" [05 //byte[] b = wc.DownloadData("http://www.baidu.com");
+ o. \. J8 X/ q; I5 L5 K06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
0 }; m. ~" |1 |$ z9 z7 b, g- l07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
9 d) N# D, y- F: e( j08 //html = html.Substring(0, html.IndexOf("</p>"));
/ ~# I& N. M! _, b. h) |09 //Response.Write(html);
1 Q7 f l( P% r% E; L) p10
0 m9 U9 j# L; w5 ]11 //方法二:
2 s3 g4 U2 K' [& A# H12 //获取整个网页
& z7 y. r% x+ F0 ?7 \# j, y4 d) v13 System.Net.WebClient wc = new System.Net.WebClient();
% q; J% ^8 k( r14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
* \) J6 O8 U% b4 Z15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
" \( V' O V" K3 E16 string html = sr.ReadToEnd();
3 O! [! o! a; Z1 a E* K2 \17 sr.Close();
+ T$ r, a# A( Q! }2 f9 ?& H+ ^# O18 //根据规则获取想要的内容
1 b, v, K4 n$ P% [" Z0 p$ H+ H19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
; P! {+ j3 Y( R: u; @' ^20 html = html.Substring(0, html.IndexOf("</p>"));
2 s) l' m6 `, N. F6 b+ ^; [21 Response.Write(html);
& `. b+ ] C( s22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (https://www.laomiw.com/) | Powered by Discuz! X3.4 |