Get specific data from a webpage

£可爱£侵袭症+ 提交于 2019-12-24 09:03:30

问题


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 succeeded in get the whole web page with this:

WebRequest request = WebRequest.Create("http://www1.caixa.gov.br/loterias/loterias/ultimos_resultados.asp");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
    html = sr.ReadToEnd();
}

After that, I can't select just these number from the HTML.


回答1:


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

public async Task<List<string>> GetNumbers()
{
    // Getting the number of microseconds since Jan 1st, 1970
    var microseconds = (long)(DateTime.UtcNow - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalMilliseconds;
    // Creating the webrequest and passing the parameter
    var 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 loop
    var jar = new CookieContainer();
    request.CookieContainer = jar;

    try
    {
        var response = await request.GetResponseAsync();
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            var html = await sr.ReadToEndAsync();
            var document = new HtmlAgilityPack.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" numbers
            return numbersNodes.Select(node => node.InnerText).Skip(6).ToList();
        }
    }
    catch (Exception e)
    {
        // very basic exception handling.
        Console.WriteLine(e);
    }
    return null;
} 

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

List<string> Numbers = await GetNumbers();


来源:https://stackoverflow.com/questions/31122891/get-specific-data-from-a-webpage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!