c# using HtmlAgilityPack to get data from HTML table

流过昼夜 提交于 2020-01-02 04:31:26

问题


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:


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);  
        }
}


来源:https://stackoverflow.com/questions/4630527/c-sharp-using-htmlagilitypack-to-get-data-from-html-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!