c# - parse - HTML Agility Pack obtiene todos los atributos href de los anclajes en la página
htmlagilitypack selectnodes (1)
Está agregando el objeto HtmlNode a CheckBoxList y no el valor del atributo href . Lo que está viendo es el HtmlNode ToString() , ya que es lo mejor que puede hacer el CheckBoxList para mostrar ese objeto.
En su lugar, puede usar GetAttributeValue(string attribute, string defaultValue) para recuperar el valor del atributo href .
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
// Get the value of the HREF attribute
string hrefValue = link.GetAttributeValue( "href", string.Empty );
cbl_items.Items.Add(hrefValue);
}
Estoy tratando de agregar enlaces extraídos de un archivo HTML a un CheckBoxList ( cbl_items ).
Funciona hasta ahora, pero en lugar del enlace, el nombre del elemento se muestra como HtmlAgilityPack.HtmlNode. Intenté usar DocumentElement lugar de Node pero dijo que no existe o similar.
¿Cómo puedo obtener la URL que se mostrará en lugar de HtmlAgilityPack.HtmlNode?
Esto es lo que he intentado hasta ahora:
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
cbl_items.Items.Add(link);
}