Webpage information capturing the title and link of the blog Park

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.