【发布时间】: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