Getting information from a div in C #

4

Well, I have a code that reads the page however I need the following:

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

The idea of the code is to look for a <a> tag that has this 2 and return the link. In case, it would return: /t848p15-teste .

The code I have to read is this:

WebRequest request = WebRequest.Create("site_aqui");
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
string Texto = reader.ReadToEnd();
    
asked by anonymous 18.07.2016 / 22:35

1 answer

2

You can use the Html Agility Pack, which is a library for handling HTML.

  

Nuget: Install-Package HtmlAgilityPack

You must load the HTML text in the class that handles the same, and then you can use XPATH to search for the desired element .

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

if (links != null)
{
    var primeiroLinkAchando = links.FirstOrDefault();

    if (primeiroLinkAchando != null)
    {
        var href = primeiroLinkAchando.Attributes["href"].Value;
        // agora você pode fazer o que quizer com o href
    }

    foreach (HtmlNode link in links)
    {
        var href = link.Attributes["href"].Value;
        // agora você pode fazer o que quizer com o href
    }
}
    
19.07.2016 / 06:08