HtmlAgilityPack obtain Title and meta

c#, html-agility-pack, title

Solution

Go about it this way:

HtmlNode mdnode = htmlDoc.DocumentNode.SelectSingleNode("//meta[@name='description']");

              if (mdnode != null)
              {
                 HtmlAttribute desc;

                 desc = mdnode.Attributes["content"];
                 string fulldescription = desc.Value;
                 Console.Write("DESCRIPTION: " + fulldescription);
              }

Problem

I try to practice "HtmlAgilityPack ", but I am having some issues regarding this. here's what I coded, but I can not get correctly the title and the description of a web page ... If someone can enlighten me on my mistake :) ``` ... public static void Main(string[] args) { string link = null; string str; string answer; int curloc; // holds current location in response string url = "http://stackoverflow.com/"; try { do { HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url); HttpWReq.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"; HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); //url = null; // disallow further use of this URI Stream istrm = HttpWResp.GetResponseStream(); // Wrap the input stream in a StreamReader. StreamReader rdr = new StreamReader(istrm); // Read in the entire page. str = rdr.ReadToEnd(); curloc = 0; //WebPage result; do { // Find the next URI to link to. link = FindLink(str, ref curloc); //return the good link Console.WriteLine("Title found: " + curloc); //title = Title(str, ref curloc); if (link != null) { Console.WriteLine("Link found: " + link); using (System.Net.WebClient client = new System.Net.WebClient()) { HtmlDocument htmlDoc = new HtmlDocument(); var html = client.DownloadString(url); htmlDoc.LoadHtml(link); //chargement de HTMLAgilityPack var htmlElement = htmlDoc.DocumentNode.Element("html"); HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//meta[@name='description']"); if (node != null) { string desc = node.GetAttributeValue("content", ""); Console.Write("DESCRIPTION: " + desc); } else { Console.WriteLine("No description"); } var titleElement = htmlDoc.DocumentNode .Element("html") .Element("head") .Element("title"); if (titleElement != null) { string title = titleElement.InnerText; Console.WriteLine("Titre: {0}", title); } else { Console.WriteLine("no Title"); } Console.Write("Done"); } Console.Write("Link, More, Quit?"); answer = Console.ReadLine(); } else { Console.WriteLine("No link found."); break; } } while (link.Length > 0); // Close the Response. HttpWResp.Close(); } while (url != null); } catch{ ...} ``` Thanks in advance :)

Original source

Related problems