网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
0 `$ r! D7 J$ T; h; A03 //方法一:
: @" G7 o8 h: X B: e( x04 //System.Net.WebClient wc = new System.Net.WebClient();
! l% @1 D+ U9 J& l05 //byte[] b = wc.DownloadData("http://www.baidu.com");
# b" E0 l5 I# u+ W8 W" [06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
- ?/ q6 i& M* m07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
" Y" {! P& E, V9 E2 x# P08 //html = html.Substring(0, html.IndexOf("</p>"));
3 S* _. z! |! E6 Q. @+ m9 N! I/ N09 //Response.Write(html);
4 V4 Q) l" x% @10
. Q$ N& I/ g* a" E! V, @: ]7 V11 //方法二:
( W( h. f ]" h$ E- Y12 //获取整个网页
; @. b+ L/ S6 [9 [13 System.Net.WebClient wc = new System.Net.WebClient();
2 l: F8 }& R7 H8 n/ A' Z14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
6 I/ |8 d9 y! V. w, c! Q& G15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
) w* U; e3 _9 ?- v16 string html = sr.ReadToEnd();
8 @. T; S/ X0 w0 ?17 sr.Close();
7 e" m9 o3 D3 v/ F7 H2 f/ Y+ d18 //根据规则获取想要的内容
7 O& b' z1 C6 s+ A6 M4 h19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
) r* l4 s6 X/ q8 G/ }& w20 html = html.Substring(0, html.IndexOf("</p>"));
9 }( a& D2 n: C0 D21 Response.Write(html);
c9 j `( T6 }' V1 G1 G) ~22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (https://laomiw.com/) | Powered by Discuz! X3.4 |