本文介绍了从网页获取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个页面,对于该页面,我需要从其他页面获取值.
I have a page, and for that page I need to get the value from a other different page.
我只想在NúmerosSorteados"框中检索这6个数字.
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();
}
在那之后,我不能只从HTML中选择这些数字.
After that, I can't select just these number from the HTML.
推荐答案
这是使用 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;
}
调用该函数非常简单:
List<string> Numbers = await GetNumbers();
这篇关于从网页获取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!