Capture results using Htmlagilitypack

Asked

Viewed 535 times

1

I’m using this code to get results using the Html Agility Pack. This is how it is:

WebRequest request = WebRequest.Create(texto);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
        string Texto = reader.ReadToEnd();

        var doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(Texto);
        var links = doc.DocumentNode.SelectNodes("//a[contains(text(),'2')][@href]");

        if (links != null)
        {
            foreach (HtmlNode link in links)
            {
                var href = link.Attributes["href"].Value;
                MessageBox.Show(href);
            }
        }

The idea of the code is: Search for a tag like this:

<a href="/t439-teste">2</a>

The code I have worked correctly and picks up the results. The problem is that it is capturing the results that have 2 in their set, I want it to only capture those who have EXACTLY 2, it alone. Has as?

Thanks for your help.

1 answer

2

I wonder if it works?

var links = doc.DocumentNode.SelectNodes("//a").where(x => x.innerHTML == "2");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.