This article takes the title and link of the blog home page as an example:
The DOM tree of the homepage is displayed. Obviously, you only need to extract the DIV whose class is post_item, and then extract the sign whose class is titlelnk. This function can be implemented through the following functions:
/// <Summary> /// search for all the tags whose names are tagnames and whose attributes are attrname and whose values are attrvalue. // For example: findtagbyattr (HTML, "Div", "class", "Demo ") /// return all Div labels whose classes are demo. /// </Summary> Public static list
With the above functions, you can extract the required HTML flag. to capture, you also need a function to download the webpage:
public static String GetHtml(string url){ try { HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; req.Timeout = 30 * 1000; HttpWebResponse response = req.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); MemoryStream buffer = new MemoryStream(); Byte[] temp = new Byte[4096]; int count = 0; while ((count = stream.Read(temp, 0, 4096)) > 0) { buffer.Write(temp, 0, count); } return Encoding.GetEncoding(response.CharacterSet).GetString(buffer.GetBuffer()); } catch { return String.Empty; }}
The following describes how to use the htmltag class to capture web page information by taking the title and link of the blog homepage as an example:
Class program {static void main (string [] ARGs) {string html = htmltag. gethtml ("http://www.cnblogs.com"); List
The running result is as follows: