网页采集程序(超级简单版)01 protected void btn_click(object sender, EventArgs e)
02 {
03 //方法一:
04 //System.Net.WebClient wc = new System.Net.WebClient();
05 //byte[] b = wc.DownloadData("http://www.baidu.com");
06 //string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
07 //html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
08 //html = html.Substring(0, html.IndexOf("</p>"));
09 //Response.Write(html);
10
11 //方法二:
12 //获取整个网页
13 System.Net.WebClient wc = new System.Net.WebClient();
14 System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
15 System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
16 string html = sr.ReadToEnd();
17 sr.Close();
18 //根据规则获取想要的内容
19 html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
20 html = html.Substring(0, html.IndexOf("</p>"));
21 Response.Write(html);
22 }
使用道具 评分 举报
http://hereson.javaeye.com/blog/207468
本版积分规则 发表回复 回帖后跳转到最后一页
0关注
18粉丝
1181帖子