c# - Get specific data from a webpage -
i have page, , page need value other different page.
i want retrieve 6 numbers "números sorteados" box.
so far succeeded in whole web page 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, can't select these number html.
here's quick way numbers using htmlagilitypack:
public async task<list<string>> getnumbers() { // getting number of microseconds since jan 1st, 1970 var microseconds = (long)(datetime.utcnow - (new datetime(1970, 1, 1, 0, 0, 0))).totalmilliseconds; // creating webrequest , passing parameter var request = webrequest.createhttp( string.format( "http://www1.caixa.gov.br/loterias/loterias/megasena/megasena_pesquisa_new.asp?app={0}", microseconds)); // adding cookie container otherwise stuck in 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 last 6 nodes represent "números sorteados" numbers return numbersnodes.select(node => node.innertext).skip(6).tolist(); } } catch (exception e) { // basic exception handling. console.writeline(e); } return null; }
and call function it's easy as:
list<string> numbers = await getnumbers();
Comments
Post a Comment