Skip to content Skip to sidebar Skip to footer

Get Specific Data From A Webpage

I have a page, and for that page I need to get the value from a other different page. I just want to retrieve the 6 numbers into the 'Números Sorteados' box. So far I only succee

Solution 1:

Here's a quick way to get the numbers using HTMLAgilityPack:

publicasyncTask<List<string>> GetNumbers()
{
    // Getting the number of microseconds since Jan 1st, 1970var microseconds = (long)(DateTime.UtcNow - (newDateTime(1970, 1, 1, 0, 0, 0))).TotalMilliseconds;
    // Creating the webrequest and passing the parametervar request =
        WebRequest.CreateHttp(
            string.Format(
                "http://www1.caixa.gov.br/loterias/loterias/megasena/megasena_pesquisa_new.asp?app={0}",
                microseconds));
    // Adding a cookie container otherwise you will be stuck in a redirect loopvar jar = newCookieContainer();
    request.CookieContainer = jar;

    try
    {
        var response = await request.GetResponseAsync();
        using (var sr = newStreamReader(response.GetResponseStream()))
        {
            var html = await sr.ReadToEndAsync();
            vardocument = newHtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            var nodes = document.DocumentNode.SelectNodes("//span [@class=\"num_sorteio\"]");
            var numbersNodes = nodes.Last().SelectNodes("//li");

            // selecting the last 6 nodes that represent the "Números Sorteados" numbersreturn numbersNodes.Select(node => node.InnerText).Skip(6).ToList();
        }
    }
    catch (Exception e)
    {
        // very basic exception handling.Console.WriteLine(e);
    }
    returnnull;
} 

and to call the function it's as easy as:

List<string> Numbers = await GetNumbers();

Post a Comment for "Get Specific Data From A Webpage"