mardi 28 avril 2015

Error while trying to get data using NHtmlUnit

I'm trying to get data that is loaded dynamically from an url. (After disabling javascript and refreshing in chrom data is no more loaded).

I tried HtmlAgilityPack but I think it is not possible so I turned to NHtmlUnit.

I'm using this code:

    public void CollectData()
    {
        string urlAddress = "http://ift.tt/1JPDe2Q";
        var client = new WebClient();
        var currentPage = (HtmlPage) client.GetPage(urlAddress);
        client.WaitForBackgroundJavaScript(10000);
        var streamWriter = new StreamWriter(@"Documents");

        for (int i = 0; i < 2; i++)
        {
            HtmlTable table = (HtmlTable) currentPage.GetByXPath("//table[@class=' table-main']")[i];
            foreach (var row in table.Rows)
            {
                foreach (var cell in row.Cells)
                {
                    streamWriter.WriteLine(cell.AsText());
                }
                streamWriter.WriteLine();
            }
            streamWriter.Close();
            client.CloseAllWindows();
        }

    }

It seems that still the used xpath give no results ... I have no idea how to fix it ..

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire