网页采集程序(超级简单版)
01 protected void btn_click(object sender, EventArgs e)
02 {
3 ~ t+ }1 A7 F! q% }9 k$ K8 x03 //方法一:
5 M! o3 P$ X! q5 x! ]04 //System.Net.WebClient wc = new System.Net.WebClient();
: O2 G7 }) \1 A+ d. u+ W- }05 //byte[] b = wc.DownloadData("http://www.baidu.com");
1 n% C( `, C. j06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
1 B/ S* D- J% L$ l# Q07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
% }: \: Z5 ~( C* Z08 //html = html.Substring(0, html.IndexOf("</p>"));
$ w+ j$ H% t" }9 A09 //Response.Write(html);
* R' R/ U& P: O: k10
# u; ^. l3 R, z" F) }11 //方法二:
6 e2 C6 y0 a% j- x, b. X12 //获取整个网页
! K9 w }% E/ A3 l4 n13 System.Net.WebClient wc = new System.Net.WebClient();
: ^4 G1 R+ J: U: U/ i14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
) y( L- R2 o5 r7 d; u* z2 Q4 }15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
' m0 d K' ]9 Q6 t5 G) j3 C16 string html = sr.ReadToEnd();
! n. l3 l/ b6 B8 G; P# f17 sr.Close();
! g$ k% t# }- y18 //根据规则获取想要的内容
/ s! `/ ?( @8 D$ _1 B& A4 Y$ j19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
- S. H$ g/ E# [, `20 html = html.Substring(0, html.IndexOf("</p>"));
9 _- o& s# L, v V# l' l n* _- k$ C21 Response.Write(html);
! g! S0 x3 D/ |# B8 ~22 }
http://hereson.javaeye.com/blog/207468
| 欢迎光临 老秘网_材夜思范文 (https://laomiw.com/) | Powered by Discuz! X3.4 |