Sélection de valeurs d’atsortingbuts avec html Agility Pack

J’essaie de récupérer une image spécifique d’un document HTML à l’aide du pack d’agilité HTML et de ce xpath:

//div[@id='topslot']/a/img/@src 

Autant que je sache, il trouve l’atsortingbut src, mais renvoie le tag img. Pourquoi donc?

Je m’attendrais à ce que InnerHtml / InnerText ou quelque chose soit défini, mais les deux sont des chaînes vides. OuterHtml est défini sur le tag img complet.

Existe-t-il une documentation pour Html Agility Pack?

HTML Agility Pack ne prend pas en charge la sélection d’atsortingbuts.

Vous pouvez directement récupérer l’atsortingbut si vous utilisez plutôt HtmlNavigator .

 //Load document from some html ssortingng HtmlDocument hdoc = new HtmlDocument(); hdoc.LoadHtml(htmlContent); //Load navigator for current document HtmlNodeNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator(); //Get value from given xpath ssortingng xpath = "//div[@id='topslot']/a/img/@src"; ssortingng val = navigator.SelectSingleNode(xpath).Value; 

Vous pouvez utiliser la méthode “GetAtsortingbuteValue”.

Exemple:

 //[...] code before needs to load a html document HtmlAgilityPack.HtmlDocument htmldoc = e.Document; //get all nodes "a" matching the XPath expression HtmlNodeCollection AllNodes = htmldoc.DocumentNode.SelectNodes("*[@class='item']/p/a"); //show a messagebox for each node found that shows the content of atsortingbute "href" foreach (var MensaNode in AllNodes) { ssortingng url = MensaNode.GetAtsortingbuteValue("href", "not found"); MessageBox.Show(url); } 

Lire et écrire des atsortingbuts avec le pack d’agilité HTML

Vous pouvez à la fois lire et définir les atsortingbuts dans HtmlAgilityPack. Cet exemple sélectionne la balise et sélectionne l’atsortingbut “lang” (langue) s’il existe, puis lit et écrit sur l’atsortingbut “lang”.

Dans l’exemple ci-dessous, le document doc.LoadHtml (this.All), “this.All” est une représentation sous forme de chaîne d’un document HTML.

Lire et écrire:

  HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(this.All); ssortingng language = ssortingng.Empty; var nodes = doc.DocumentNode.SelectNodes("//html"); for (int i = 0; i < nodes.Count; i++) { if (nodes[i] != null && nodes[i].Attributes.Count > 0 && nodes[i].Atsortingbutes.Contains("lang")) { language = nodes[i].Atsortingbutes["lang"].Value; //Get atsortingbute nodes[i].Atsortingbutes["lang"].Value = "en-US"; //Set atsortingbute } } 

Lecture seulement:

  HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(this.All); ssortingng language = ssortingng.Empty; var nodes = doc.DocumentNode.SelectNodes("//html"); foreach (HtmlNode a in nodes) { if (a != null && a.Atsortingbutes.Count > 0 && a.Atsortingbutes.Contains("lang")) { language = a.Atsortingbutes["lang"].Value; } } 

J’ai utilisé le moyen suivant pour obtenir les atsortingbuts d’une image.

 var MainImageSsortingng = MainImageNode.Atsortingbutes.Where(i=> i.Name=="src").FirstOrDefault(); 

Vous pouvez spécifier le nom de l’atsortingbut pour obtenir sa valeur. Si vous ne connaissez pas le nom de l’atsortingbut, définissez un point d’arrêt après avoir récupéré le nœud et consultez ses atsortingbuts en survolant celui-ci.

J’espère que j’ai aidé.