Retrieve data from HTML table in C#

后端 未结 2 980
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 07:15

I want to retrieve data from HTML document. I am scraping data from a web site I almost done but get issue when tried to retrieve data from the table. Here is HTML code

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-19 07:42

    Don't you have any control over the page being displayed within the Webbrowser control? If you do it's better you add an id field for status TD. Then your life would be much easier.

    Anyway, here's how you could search a value within a table.

    HtmlElementCollection tables = this.WB.Document.GetElementsByTagName("table");
    
                foreach (HtmlElement TBL in tables)
                {
                    foreach (HtmlElement ROW in TBL.All)
                    {
    
                        foreach (HtmlElement CELL in ROW.All)
                        {
    
                            // Now you are looping through all cells in each table
    
                            // Here you could use CELL.InnerText to search for "Status" or "Approved"
                        }
                    }
                }
    

    But, this is not a good approach as you are looping through each table and each cell within each table to find your text. Keep this as the last option.

    Hope this helps you to get an idea.

提交回复
热议问题