c# using HtmlAgilityPack to get data from HTML tab

2019-05-02 20:58发布

i am trying to get information out of an html table by parsing the html using HtmlAgilityPack.

here is what the HTML looks like:

...
...
...
<tbody>
                    <tr>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">AA00857</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div></div>
                            <div class="style_20">TPRCF</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21"></div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21">16908/2</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">&nbsp;ETG_C</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">AA01231</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div></div>
                            <div class="style_20">TPRCF</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21"></div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21">16909/19</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">&nbsp;ETG_C</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">AA01233</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div></div>
                            <div class="style_20">TPRCF</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21"></div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_21">16907/7</div>
                        </td>
                        <td class="style_19" style="vertical-align: baseline;">
                            <div class="style_18">&nbsp;ETG_C</div>
                        </td>
                    </tr>
...
...

i need to extract from the above these values:

AA00857, TPRCF, 16908/2, ETG_C

so far all i have is this:

HtmlWeb hw = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(@"http://www.some123123site.com/index");



            if (htmlDoc.DocumentNode != null)
            {
                HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//tbody");

                if (bodyNode != null)
                {
                    // Do something with bodyNode
                }
            }

please help!

1条回答
叛逆
2楼-- · 2019-05-02 21:33

Try this:

HtmlWeb hw = new HtmlWeb();              
HtmlAgilityPack.HtmlDocument htmlDoc = hw.Load(@"http://www.some123123site.com/index");                 
if (htmlDoc.DocumentNode != null)              
{                   
        foreach(HtmlNode text in htmlDoc.DocumentNode.SelectNodes("//tr/td/div/text()"))
        {     
            Console.WriteLine(text.InnerText);  
        }
}
查看更多
登录 后发表回答