网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
- s% L. Z) r1 @/ F8 R/ ?7 Y03 //方法一:
) z& D+ H k7 U7 E04 //System.Net.WebClient wc = new System.Net.WebClient();
3 Y/ K3 y7 F, E" q5 G5 u05 //byte[] b = wc.DownloadData("http://www.baidu.com");
1 f6 m% l4 r- P6 v7 j+ ~06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
& R: z) k) L Y/ A) b$ L07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
P) R$ C( a) r1 N08 //html = html.Substring(0, html.IndexOf("</p>"));
8 ^% b6 s6 ] D( e09 //Response.Write(html);
[+ q5 q4 `% L- l+ N0 s- i' C10
. @( t9 B, ^9 @/ w: A' ]/ t11 //方法二:
+ n1 }! i f" H3 T q0 G12 //获取整个网页
: E5 W8 J/ }% K6 H% p/ r' D13 System.Net.WebClient wc = new System.Net.WebClient();
9 Z8 L+ d. F3 `' e. |0 u' B14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
. @: B. {5 c& {3 P15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
% G) N3 h9 Q4 j1 y4 g$ ~! v2 p. M16 string html = sr.ReadToEnd();
8 c. p, Z5 a9 z3 {* ]/ Y17 sr.Close();
" d" h! f' Z+ K. g% @18 //根据规则获取想要的内容
1 s/ f/ l1 `; d5 q2 h% y5 g, I19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
% N0 h; G3 ?" K+ z S20 html = html.Substring(0, html.IndexOf("</p>"));
4 S* |& {: W1 s6 c: C2 N21 Response.Write(html);
6 \; { L: F" D6 f$ }2 T22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) | Powered by Discuz! X3.4 |