我无法使用C#获取实时数据。

4
我希望使用htmlagilitypack来获取实时数据,需要即时获取加密货币价格。我在项目中使用定时器实现每5秒刷新,但总是得到第一个值。例如,dolar现在是8.65try,但15秒后变为8.70try。我得到的数据始终是第一个值8.65try。
private void button1_Click(object sender, EventArgs e)
{
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    Uri url = new Uri("https://www.bloomberght.com/borsa");
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    string html = client.DownloadString(url);
    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    document.LoadHtml(html);
    HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//a[@id='dolar']/span[@class='data-info']/small[@data-type='son_fiyat']");

    foreach (var item in nodes)
    {
        string link = item.Attributes["class"].Value;
        label1.Text = item.InnerText;
        listBox1.Items.Add(item.InnerText);
    }
}

3
除去技术问题,我怀疑你正在违反该网站的服务条款,如果你的爬虫被发现,可能会面临反制措施。我认为,如果你需要实时数据服务,应该使用付费的API服务。 - Filburt
1个回答

4

您可能正在访问缓存。

尝试为您的请求指定BypassCache级别:

client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接