HTML Agility Pack get all anchors' href attributes on page

前端 未结 1 1232
挽巷
挽巷 2021-01-01 13:23

I am trying to add links extracted from an HTML file to a CheckBoxList (cbl_items).

It works so far but instead of the link, the item\'s na

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 14:11

    You are adding the HtmlNode object to the CheckBoxList and not the value of the href attribute. What you are seeing is the HtmlNode's ToString() value since that's the best that the CheckBoxList can do to display that object.

    Instead, you can use GetAttributeValue(string attribute, string defaultValue) to retrieve the href attribute's value.

    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);
    }
    

    0 讨论(0)
提交回复
热议问题