简体   繁体   English

C# HTML 解析

[英]C# Html parsing

I'm trying to parse HTML in my C# project without success, I am using a HtmlAgilityPack lib to do so, I can get some of the HTML body text but not all of it for some reason.我试图在我的 C# 项目中解析 HTML 没有成功,我正在使用 HtmlAgilityPack 库来这样做,我可以得到一些 HTML 正文,但由于某种原因不是全部。 I need to grab the div with ID of latestPriceSection, and filter to the USD value from https://www.monero.how/widget我需要获取带有 latestPriceSection ID 的 div,并从https://www.monero.how/widget过滤到 USD 值

My function (doesn't work)我的功能(不起作用)

public void getXMRRate()
{
    HtmlWeb web = new HtmlWeb();
    HtmlAgilityPack.HtmlDocument document = web.Load("https://www.monero.how/widget");
    HtmlNode[] nodes = document.DocumentNode.SelectNodes("//a").Where(x => x.InnerHtml.Contains("latestPriceSection")).ToArray();
    foreach (HtmlNode item in nodes)
    {
        Console.WriteLine(item.InnerHtml);
    }
}

Your function doesn't work because the widget is updated via script.您的功能不起作用,因为小部件是通过脚本更新的。 The div contains nothing when you load the page.加载页面时,div 不包含任何内容。 You can't use HAP to scrape the information of this.您不能使用 HAP 来抓取此信息。 Find a web service that can give you the information you need.查找可以为您提供所需信息的 Web 服务。

Alternatively you can use Selenium to get the HTML after the page has loaded the script.或者,您可以在页面加载脚本后使用 Selenium 获取 HTML。 Or you the WebBrowser class, but that requires you to have a form application where the form contains the WebBrowser.或者您是 WebBrowser 类,但这需要您有一个表单应用程序,其中表单包含 WebBrowser。

您需要从https://www.monero.how/widgetLive.json检索 JSON 数据,因为小部件在 Ajax 请求中使用此资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM