开发者

Select all links from a Html table using XPath (and HtmlAgilityPack)

开发者 https://www.devze.com 2022-12-24 15:57 出处:网络
What I am trying to achieve is to extract all links with a href attribute that starts with http://, https:// or /. These links lie within a table (tbody > tr > td etc) with a certain class. I thought

What I am trying to achieve is to extract all links with a href attribute that starts with http://, https:// or /. These links lie within a table (tbody > tr > td etc) with a certain class. I thought I could specify just the the a element without the whole path to it but it does not seem to work. I get a NullReferenceException at the line that selects the links:

var table = doc.DocumentNode.SelectSingleNode("//table[@cla开发者_JAVA百科ss='containerTable']");
if (table != null)
{
    foreach (HtmlNode item in table.SelectNodes("a[starts-with(@href, 'https://')]"))
    {
        //not working

I don't know about any recommendations or best practices when it comes to XPath. Do I create overhead when I query the document two times?


Use:

 //tbody/descendant::a[starts-with(@href,'https://')
                     or
                       starts-with(@href,'http://')
                     or
                       starts-with(@href,'./') 
                      ]

You will still have a problem, unless you correct your code to reflect the fact that the the XmlNode.SelectNodes() instance method has a return type of XmlNodeList, not HtmlNode.


The problems is that you are selecting the table and then immediately trying to select the anchors as if they were direct decedents. There are tr and td tags in the middle.

So, if you change your xpath to the following, things should work:

"tbody/tr/td/a[starts-with(@href, 'https://')]"

This will not work if your anchors are wrapped up in something else, so you could select all of the anchors in the current node set (i.e. table):

"//a[starts-with(@href, 'https://')]"

See this for more detail on xpath syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号