网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
$ _9 n, T6 j; E4 e: I% W03 //方法一:
: u# H" C2 W& W5 ?) I% }3 \04 //System.Net.WebClient wc = new System.Net.WebClient();
0 w6 E4 l4 g5 v05 //byte[] b = wc.DownloadData("http://www.baidu.com");
* a z# f% K( |# _06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
e$ B5 m; R" `07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
+ V- j- `4 y/ s: q( K) ]08 //html = html.Substring(0, html.IndexOf("</p>"));
& `, a7 Z0 Y7 T) o: t" n# i09 //Response.Write(html);
0 {4 z' c# l# ?3 L5 Z10
6 N3 _4 w* D7 |- u/ ^11 //方法二:
5 @4 }4 z8 n8 }3 P3 I. h12 //获取整个网页
" p, B8 u* {1 s2 g13 System.Net.WebClient wc = new System.Net.WebClient();
2 }! Y4 p. R5 z, ~8 O4 P2 f: k14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
, G5 u6 _$ }3 v/ }15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
- q) s0 T2 U- V# O/ p- |- ]16 string html = sr.ReadToEnd();
; ~3 P3 B: j U17 sr.Close();
( h q2 L/ h; q9 l4 U8 B18 //根据规则获取想要的内容
' D7 u7 p, J6 Q' o) G19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
q- t7 _: C( l; s; B% V20 html = html.Substring(0, html.IndexOf("</p>"));
! m- t5 L7 y/ M. K+ u21 Response.Write(html);
) q! \+ ~! {3 @ h' N# ]& c22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (http://www.laomiw.com/) | Powered by Discuz! X3.4 |