How to get information from a tr in HTML code using Htmlagilitypack from Visual Studio

Asked

Viewed 223 times

0

Good night, I’m doing a C# project at Visualstudio 2019 using Htmlagillypack to capture site information https://www.infomoney.com.br/cotacoes/ibovespa/ which contains the values of the stock exchange. I don’t know which elements to use to get the tags I need, below is the photo of the html code of my code C#.

NOTE: in my program I am using a Messagebox.Show only to perform the tests easier, when I can access them I will arrange each one in its proper variable.

I don’t know I could access the three inside my tbody'.

Thank you very much in advance! Html e Pagina

Codigo C#

1 answer

1


Marcelo, I set an example here for you to read the td but you will need to make some negotiations that are commented in the code below:

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument()
{
    OptionFixNestedTags = true
};

// TODO Carregar o HTML dinamicamente através de um requisição
htmlDoc.LoadHtml(@"
    <div class=""col-12 col-lg-8"">
        <div class=""data-table-full"">
        <table id=""high"" class=""default-table active"">
            <thead>
                <tr>
                    <th>Ativo</th>
                    <th>Último (R$)</th>
                    <th>Var. Dia (%)</th>
                    <th>Val. Min (R$)</th>
                    <th>Val. Máx (R$)</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tr>
                <td><a href=""https://www.infomoney.com.br/AZUL4"">AZUL4</a></td>
                <td>52,90</td>
                <td class=""positive"">1.13</td>
                <td>52,46</td>
                <td>53,55</td>
                <td>11:01 25/10</td>
            </tr>
            <tr>
                <td><a href=""https://www.infomoney.com.br/BBAS3"">BBAS3</a></td>
                <td>47,30</td>
                <td class=""positive"">0.76</td>
                <td>47,12</td>
                <td>48,05</td>
                <td>11:01 25/10</td>
            </tr>
        </table>
        </div>
    </div>
    ");

var trs = htmlDoc
    .GetElementbyId("high")
    .ChildNodes
    .Where(a => a.Name == "tr");

foreach(var tr in trs)
{
    // TODO Aqui você precisará efetuar algumas tratativas:
    // Se dentro do TD houver um outro elemento, você precisará
    // descer mais um nível para pegar o innerText.
    if (tr.ChildNodes[1].Name == "a")
    {
        // Tratar o anchor
        var innerTextDoAnchor = tr.ChildNodes[0].ChildNodes[0].InnerText;
    }

    // Exemplo de como concatenar os InnerText
    string valoresDosTdsConcatenados = string.Join(", ", tr.ChildNodes.Select(a => a.InnerText));
}
  • 1

    Xará, saved me legal... Gave right if code, thanks for the attention, was of great help.

Browser other questions tagged

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