您的位置:首页 > 其它

网页采集程序

2010-06-30 21:31 120 查看
大热天气闷死了,没事写个简单的网页采集程序(超级简单版)

protected void btn_click(object sender, EventArgs e)
{
//方法一:
//System.Net.WebClient wc = new System.Net.WebClient();
//byte[] b = wc.DownloadData("http://www.baidu.com");
//string html = System.Text.Encoding.GetEncoding("gb2312").GetString(b);
//html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
//html = html.Substring(0, html.IndexOf("</p>"));
//Response.Write(html);

//方法二:
//获取整个网页
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream sm = wc.OpenRead("http://www.baidu.com");
System.IO.StreamReader sr = new System.IO.StreamReader(sm, System.Text.Encoding.Default, true, 256000);
string html = sr.ReadToEnd();
sr.Close();
//根据规则获取想要的内容
html = html.Substring(html.IndexOf("<p id=\"lg\">") + "<p id=\"lg\">".Length);
html = html.Substring(0, html.IndexOf("</p>"));
Response.Write(html);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: