HTML敏捷包获取网页上的所有URL [英] HTML Agility pack get all URLs on page

查看:240
本文介绍了HTML敏捷包获取网页上的所有URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加从HTML文件提取到一个链接的CheckBoxList cbl_items )。



它的工作原理,但迄今为止,而不是链接,该项目的名称显示为HtmlAgilityPack.HtmlNode。
我试图使用 DocumentElement 而不是节点,但它说,它不存在或相似的。



我怎样才能显示,而不是HtmlAgilityPack.HtmlNode的网址是什么?



这是我已经试过到目前为止:

  HtmlWeb HW =新HtmlWeb(); 
HtmlAgilityPack.HtmlDocument DOC =新HtmlAgilityPack.HtmlDocument();
DOC = hw.Load(tb_url.Text);
的foreach(在doc.DocumentNode.SelectNodes(HtmlNode链接//一个[@href]))
{
cbl_items.Items.Add(链接);
}


解决方案

您要添加的 HtmlNode 对象应用于的CheckBoxList ,而不是的href的值属性。你们看到的是 HtmlNode 的ToString()值,因为这是最好的的CheckBoxList 可以做,以显示该对象。



相反,你可以使用 GetAttributeValue(字符串属性,字符串设置defaultValue)来检索 。HREF 属性的值

  HtmlWeb HW =新HtmlWeb(); 
HtmlAgilityPack.HtmlDocument DOC =新HtmlAgilityPack.HtmlDocument();
DOC = hw.Load(tb_url.Text);
的foreach(在doc.DocumentNode.SelectNodes(HtmlNode链接//一个[@href]))
{
//获取HREF属性
字符串的值hrefValue = link.GetAttributeValue(HREF的String.Empty);
cbl_items.Items.Add(hrefValue);
}


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 name is displayed as HtmlAgilityPack.HtmlNode. I tried using DocumentElement instead of Node but it said that it does not exist or similar.

How can I get the URL to be displayed instead of HtmlAgilityPack.HtmlNode?

This is what I've tried so far:

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

解决方案

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

这篇关于HTML敏捷包获取网页上的所有URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆