网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
# Y$ ^2 I3 S/ ?% @4 C8 D03 //方法一:
1 \9 R* i7 v, H# q: |& R. F04 //System.Net.WebClient wc = new System.Net.WebClient();
7 Q' Z7 L9 x% p- n/ v/ ^05 //byte[] b = wc.DownloadData("http://www.baidu.com");
0 [& O/ G- w! m: q06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
$ Z% L( I3 r. F5 \07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
. f( g* R M" r. V- f08 //html = html.Substring(0, html.IndexOf("</p>"));
: x2 F) C8 g7 `# I; Y09 //Response.Write(html);
# a8 p1 K: \* V' Y10
8 P z" z3 I" I5 D+ X) \; }5 M11 //方法二:
e$ g) \* z9 ]( P9 T: y! c" k12 //获取整个网页
/ k% K p- l4 g. I13 System.Net.WebClient wc = new System.Net.WebClient();
# `. t" n5 s$ T. [, |( l+ ]14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
0 F+ @1 |5 N/ k# G15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
' Z, r9 }8 i: f1 {8 @- n( x$ ~7 y7 w) n16 string html = sr.ReadToEnd();
2 j7 M4 @5 X) J/ S6 D7 Q8 B17 sr.Close();
1 u6 g7 t* E ?; U: h$ G18 //根据规则获取想要的内容
& X& h0 y1 b B" n% J19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
[3 h0 Q4 R0 [0 M% K20 html = html.Substring(0, html.IndexOf("</p>"));
# B7 u7 O" S. c- ^21 Response.Write(html);
- [% q1 o2 g1 |' H$ u! Q9 s* ^22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (https://laomiw.com/) | Powered by Discuz! X3.4 |