网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
4 e+ }4 G! X4 w, P03 //方法一:
8 ]: ?) K' m1 W$ ~( q1 _% Z+ W04 //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 s06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
6 R0 m) ]5 ~; |$ N% k4 Y/ C07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
( _6 f0 t2 d- y0 ^7 r* h08 //html = html.Substring(0, html.IndexOf("</p>"));
s5 n& Z5 J8 [8 e4 S09 //Response.Write(html);
# @) d3 H3 [+ ^- M6 K! l10
2 y. a) g- D `0 T11 //方法二:
# M0 j# J* ~0 x; ~12 //获取整个网页
8 J% M# c1 a. H& c' r: B0 c+ V13 System.Net.WebClient wc = new System.Net.WebClient();
- K/ e, k. w5 n; g. h! w14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
+ } ~( S, i: c# e9 Y; Q) R15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
4 m" t- ]# b+ U! M4 S, Z16 string html = sr.ReadToEnd();
1 Y- J! B- y) }. M' V$ N( X; h6 e( B17 sr.Close();
3 d' {2 J# Y# S3 e" u$ s5 m18 //根据规则获取想要的内容
0 c+ P( m0 a' H) H- e19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
/ q+ [" d( e) I+ n3 S+ V0 `4 N: g. t20 html = html.Substring(0, html.IndexOf("</p>"));
& ~, j y% f4 O21 Response.Write(html);
% \! s0 U) N0 i% D1 x2 c0 E7 i22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) | Powered by Discuz! X3.4 |