【问题标题】:HTML Agility can't parse <span> when it changesHTML Agility 在更改时无法解析 <span>
【发布时间】:2022-12-18 07:41:50
【问题描述】:

我正在尝试使用 HTML Agility Pack 解析货币对的价格,我能够在首次解析时解析价格,但价格会定期变化。

            string asset = cmbPair.Text.ToString();
            var html = @"https://markets.businessinsider.com/currencies/" + asset;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
            HtmlWeb web = new HtmlWeb();
            web.CacheOnly = false;

            var htmlDoc = web.Load(html);
            //All 3 Nodes
            var node = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='price-section__values']").InnerText;
            //Singular Nodes
            var onlyprice = htmlDoc.DocumentNode.SelectSingleNode("//span[starts-with(@class, 'price-section__current-value')]").InnerText; //Need this when it updates
            var onlypricechange = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='price-section__absolute-value']").InnerText;
            var onlyperchange = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='price-section__relative-value']").InnerText;

            
            //htmlDoc.DocumentNode.SelectSingleNode("//span[@class='price-section__current-value price-section__current-value--positive-updated']").InnerText;
            //htmlDoc.DocumentNode.SelectSingleNode("//span[@class='price-section__current-value price-section__current-value--negative-updated']").InnerText;

如前所述,价格确实会解析,但当价格发生变化时不会解析,我已经使用 Visual Studio 上的计时器每 500 毫秒运行一次函数,但它不会更新价格。

我已经使用 inspect 元素检查了网站的代码,并注意到如果价格上涨,price-section__current-value 会变为 price-section__current-value price-section__current-value--positive-updated,而当价格下降时,price-section__current-value price-section__current-value--negative-updated 会变为 price-section__current-value price-section__current-value--negative-updated。为了尝试让节点解析,我使用了 starts-with 函数,它不会改变任何东西。

对我的代码的任何帮助将不胜感激!

【问题讨论】:

    标签: c# visual-studio visual-studio-code html-agility-pack


    【解决方案1】:

    您在检查实时页面时看到的任何更改都可能无关紧要。当 HTTP 客户端(在本例中为 HtmlWeb)请求页面时,它将收到一个响应,仅此而已。如果您的代码第一次有效,那么它第二次也应该有效。

    您执行后续请求的方式可能有问题,但您的代码示例不包含该部分。

    对于它的价值,这是我在周末测试过的工作示例:

    var asset = "eur-usd";
    string html = @"https://markets.businessinsider.com/currencies/" + asset;
    
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    var web = new HtmlWeb
    {
        CacheOnly = false
    };
    
    var counter = 0;
    while (++counter <= 10)
    {
        HtmlDocument doc = web.Load(html);
    
        string onlyprice = doc.DocumentNode.SelectSingleNode("//span[contains(@class, 'price-section__current-value')]").InnerText; //Need this when it updates
        string onlypricechange = doc.DocumentNode.SelectSingleNode("//span[contains(@class, 'price-section__absolute-value')]").InnerText;
        string onlyperchange = doc.DocumentNode.SelectSingleNode("//span[contains(@class, 'price-section__relative-value')]").InnerText;
    
        Console.WriteLine($"Attempt {counter}");
        Console.WriteLine($"price-section__current-value: {onlyprice}");
        Console.WriteLine($"price-section__absolute-value: {onlypricechange}");
        Console.WriteLine($"price-section__relative-value: {onlyperchange}");
        Console.WriteLine("");
        
        Thread.Sleep(500);
    }
    

    请注意,我已将您的选择器更改为使用 contains 函数。

    如果在交易时间(相对于周末)spans 上的类确实发生了变化,这取决于价格变动,那么您可能需要相应地修改您的 XPath 选择器以使其更加健壮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 2014-07-13
      • 2014-08-31
      相关资源
      最近更新 更多