Parsing HTML with NSoup

1. Add NSoup package package NuGet

public  static  void TestNSoup ( String urlStr) 
        { 
            the WebClient WC = new new the WebClient ();
             byte [] = htmlByte wc.DownloadData (urlStr);
             String HTML = Encoding.UTF8.GetString (htmlByte);
             // declare Document Object 
            Document DOC = NSoupClient .Parse (HTML); 

            // directly url Document Object obtained by 
            the Document doc1 = NSoupClient.Connect (urlStr) .Get (); 

            Elements div = doc1.GetElementById ( " tag ID " ) .GetElementsByTag (" Tag name such as, div " ); 

            // get the elements by ID 
            the Element userNames = doc.GetElementById ( " tag ID " );
             // Get the name of the element through the label 
            Elements doc.GetElementsByTag P = ( " P " ); 

            // by Get class style elements 
            elements btnNews = doc.GetElementsByClass ( " BTN-News " ); 

            // acquired by the attribute 
            elements attrstyle = doc.GetElementsByAttribute ( " style " ); 

            // traversing the acquired label 
            the foreach (the element Item in  div)
            {
                Console.WriteLine(item.Html());
            }
        }

 

Guess you like

Origin www.cnblogs.com/sanday/p/11430286.html