从网页中解析特定数据

时间:2014-03-21 12:30:59

标签: c# html-agility-pack

我使用以下代码块从this webpage获取数据。 我认为这个代码块是不合理的,因为我曾经使用bordercolor的表。 但我找不到获取数据的不同方法。有没有不同的方式,因为我是C#的新手。

感谢您的帮助。

foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table[@bordercolor='#3366cc']/tr"))
{
    sXPath = node.XPath + "/td[2]/font[1]";
    htmlNode = document.DocumentNode.SelectSingleNode(sXPath);

    if(htmlNode != null)
    {
        if (htmlNode.InnerText.Length >= 7)
        {
            string freq = htmlNode.InnerText.Substring(0, 5);
            if (int.TryParse(freq, out intFrequency) == true)
            {
                string pol = htmlNode.InnerText.Substring(6, 1);
                if (pol == "H")
                    bPolarity = false;
                else if (pol == "V")
                    bPolarity = true;
            }
        }
    }

    sXPath = node.XPath + "/td[3]/font[1]";
    htmlNode = document.DocumentNode.SelectSingleNode(sXPath);

    if (htmlNode != null)
    {
        if (htmlNode.InnerText.Length >= 5)
        {
            string sr = htmlNode.InnerText.Substring(0, 5);
            if (int.TryParse(sr, out intSymbolRate) == false)
            {
                sr = htmlNode.InnerText.Substring(0, 4);
                int.TryParse(sr, out intSymbolRate);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用Web客户端()或HTTP请求从网站获取更简单的内容

示例代码:

string winPhoneGeekTweetsUrl = @"http://sss.sss78.sset/wsss.php";
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(winPhoneGeekTweetsUrl));

使用Downloadstringcompleted事件处理程序

进行剩余的功能

答案 1 :(得分:0)

我找到了不同的方式,我认为这更好更强。

foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table/tr/td/font"))
{
    if (node.InnerText == "Freq PolMode" || node.InnerText == "SR-FEC")
    {
        sXPath = node.ParentNode.ParentNode.ParentNode.XPath + "//tr";
        HtmlNodeCollection rows = document.DocumentNode.SelectNodes(sXPath);
        for (int i = 0; i < rows.Count; i++)
        {
            sXPath = rows[i].XPath + "/td[2]/font[1]";
            htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
            if (htmlNode != null)
            {
                if (htmlNode.InnerText.Length >= 7)
                {
                    string freq = htmlNode.InnerText.Substring(0, 5);
                    if (int.TryParse(freq, out intFrequency) == true)
                    {
                        string pol = htmlNode.InnerText.Substring(6, 1);
                        if (pol == "H")
                            bPolarity = false;
                        else if (pol == "V")
                             bPolarity = true;
                     }
                 }
             }

             sXPath = rows[i].XPath + "/td[3]/font[1]";
             htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
             if (htmlNode != null)
             {
                 if (htmlNode.InnerText.Length >= 5)
                 {
                     string sr = htmlNode.InnerText.Substring(0, 5);
                     if (int.TryParse(sr, out intSymbolRate) == false)
                     {
                         sr = htmlNode.InnerText.Substring(0, 4);
                         int.TryParse(sr, out intSymbolRate);
                     }
                 }
             }
         }
         break;
     }
 }