Sto cercando di recuperare un’immagine specifica da un documento html, usando il pacchetto agility html e questo xpath:
//div[@id='topslot']/a/img/@src
Per quanto posso vedere, trova l’attributo src, ma restituisce il tag img. Perché?
Mi aspetto che InnerHtml / InnerText o qualcosa sia impostato, ma entrambe sono stringhe vuote. OuterHtml è impostato sull’img-tag completo.
Ci sono documenti per Html Agility Pack?
Html Agility Pack non supporta la selezione degli attributi.
Puoi prendere direttamente l’attributo se invece usi HtmlNavigator
.
//Load document from some html string HtmlDocument hdoc = new HtmlDocument(); hdoc.LoadHtml(htmlContent); //Load navigator for current document HtmlNodeNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator(); //Get value from given xpath string xpath = "//div[@id='topslot']/a/img/@src"; string val = navigator.SelectSingleNode(xpath).Value;
È ansible utilizzare il metodo “GetAttributeValue”.
Esempio:
//[...] 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 attribute "href" foreach (var MensaNode in AllNodes) { string url = MensaNode.GetAttributeValue("href", "not found"); MessageBox.Show(url); }
Html Agility Pack lo supporterà presto.
http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=204342
Lettura e scrittura di attributi con Html Agility Pack
È ansible leggere e impostare gli attributi in HtmlAgilityPack. Questo esempio seleziona il tag e seleziona l’attributo “lang” (lingua) se esiste e quindi legge e scrive nell’attributo “lang”.
Nell’esempio seguente, doc.LoadHtml (this.All), “this.All” è una rappresentazione di stringa di un documento html.
Leggere e scrivere:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(this.All); string language = string.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].Attributes.Contains("lang")) { language = nodes[i].Attributes["lang"].Value; //Get attribute nodes[i].Attributes["lang"].Value = "en-US"; //Set attribute } }
Sola lettura:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(this.All); string language = string.Empty; var nodes = doc.DocumentNode.SelectNodes("//html"); foreach (HtmlNode a in nodes) { if (a != null && a.Attributes.Count > 0 && a.Attributes.Contains("lang")) { language = a.Attributes["lang"].Value; } }
Ho usato il seguente modo per ottenere gli attributi di un’immagine.
var MainImageString = MainImageNode.Attributes.Where(i=> i.Name=="src").FirstOrDefault();
È ansible specificare il nome dell’attributo per ottenere il suo valore; se non si conosce il nome dell’attributo, fornire un punto di interruzione dopo aver recuperato il nodo e averne visto gli attributi passandoci sopra con il mouse.
Spero di aver aiutato.