网页信息格式化方法GetAttribute()

以前格式化网页或者信息提取一般都用的是正则;昨天由于网页信息用正则实现起来太麻烦而且易出错所以改用webbrowser 然后利用GetAttribute来实现信息的格式化

HtmlElementCollection tables = webBrowser.Document.GetElementsByTagName("table");

            try
            {
                HtmlElement table1=null;
                HtmlElement table2=null;

                for (int x = 0; x < tables.Count; x++)
                {
                    try
                    {
                        if (tables[x].GetAttribute("className") == "text-f12" && tables[x].GetAttribute("width") == "100%" && tables[x].GetAttribute("cellspacing") == "2")
                        {
                            table1 = tables[x];
                        }
                        if (tables[x].GetAttribute("width") == "100%" && tables[x].GetAttribute("cellpadding") == "4" && tables[x].GetAttribute("cellspacing") == "1")
                        {
                            table2 = tables[x];
                        }
                    }
                    catch
                    {
                    }
                }


                string[] sArray = new string[15];

                string[] sArray2 = new string[14];
                sArray2[0] = "Business";
                sArray2[1] = "Year";
                sArray2[2] = "Products";
                sArray2[3] = "Website";
                sArray2[4] = "Bankers";
                sArray2[5] = "Standard";
                sArray2[6] = "Products";
                sArray2[7] = "Services";
                sArray2[8] = "Company";
                sArray2[9] = "Address";
                sArray2[10] = "Phone";
                sArray2[11] = "Fax";
                sArray2[12] = "Contact";
                sArray2[13] = "Mobile";
                for (int i = 1; i < table1.GetElementsByTagName("tr").Count; i++)
                {
                    string temp = table1.GetElementsByTagName("tr")[i].GetElementsByTagName("td")[0].InnerHtml;
                    if (table1.GetElementsByTagName("tr")[i].GetElementsByTagName("td").Count ==3)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            if (temp.IndexOf(sArray2[j]) > -1)
                            {
                                sArray[j] = table1.GetElementsByTagName("tr")[i].GetElementsByTagName("td")[2].InnerHtml.ToString();
                            }
                        }
                    }
                }
                for (int m = 0; m < table2.GetElementsByTagName("tr").Count-1; m++)
                {
                    string temp2 = table2.GetElementsByTagName("tr")[m].GetElementsByTagName("td")[0].InnerHtml;
                    if (m == 0)
                    {
                        sArray[8] = table2.GetElementsByTagName("tr")[0].GetElementsByTagName("td")[2].InnerHtml;
                    }

                    if (table2.GetElementsByTagName("tr")[m].GetElementsByTagName("td").Count==2)
                    {
                        for (int j = 8; j < 13; j++)
                        {
                            if (temp2.IndexOf(sArray2[j]) > -1)
                            {
                                sArray[j] = table2.GetElementsByTagName("tr")[m].GetElementsByTagName("td")[1].InnerHtml;
                            }
                        }
                    }
                }

                sArray[14] = id;
                AddComInfo(sArray);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lyflcear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值