日期:2014-05-18 浏览次数:21127 次
HttpWebRequest httpWebRequest = WebRequest.Create(@"http://www.cnblogs.com") as HttpWebRequest; HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; Stream stream = httpWebResponse.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); string s = reader.ReadToEnd(); reader.Close(); stream.Close(); httpWebResponse.Close(); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(s); HtmlNodeCollection anchors = htmlDoc.DocumentNode.SelectNodes(@"//a[@class='titlelnk']"); foreach (HtmlNode anchor in anchors) Console.WritleLine(anchor.InnerHtml);
------解决方案--------------------
a.下载网页数据;
b.处理网页得到想要的数据;
c.保存数据
对于a有三种方式
1.使用WebClient下载 2.使用HttpWebRequest和HttpWebResponse 3.使用WebBrowser
对于b
1.正则表达式匹配; 2.使用HtmlAgilityPack
对于c 按个人需求而定