Je suis nouveau sur htmlagilitypack, j’essaie de trouver un moyen de récupérer les liens d’un fichier HTML configuré comme celui-ci.
<div class="std"><div style="border-right: 1px solid #CCCCCC; float: left; height: 590px; width: 190px;"><div style="background-color: #eae3db; padding: 8px 0 8px 20px; font-weight: bold; font-size: 13px;">test</div>
<div>
<div style="font-weight: bold; margin: 5px 0 -6px;">FEATURED</div>
<span class="widget widget-category-link"><a href="http://www.href1.com"><span>cat1</span></a></span>
<span class="widget widget-category-link"><a href="http://www.href1.com"><span>cat2</span></a></span>
</div></div>
Je n'ai pas encore écrit de code en c #, mais je me demandais si quelqu'un pourrait indiquer quelles balises devraient pointer vers pour obtenir les liens et le texte interne lorsqu'il n'y a pas d'identifiant HTML '. Merci
Si vous êtes familier avec XPATH, vous pourrez naviguer à travers les éléments et les attributs du code HTML pour obtenir ce que vous voulez. Pour obtenir chaque href dans ce qui précède, vous pouvez écrire le code comme suit:
const string xpath = "/div//span/a";
//WebPage below is a string that contains the text of your example
HtmlNode html = HtmlNode.CreateNode(WebPage);
//The following gives you a node collection of your two <a> elements
HtmlNodeCollection items = html.SelectNodes(xpath);
foreach (HtmlNode a in items)
{
if (a.Attributes.Contains("href"))
//Get your value here
{
yourValue = a.Attributes["href"].Value
}
}
Remarque: je n'ai ni exécuté ni testé ce code.