简体   繁体   中英

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. I need to grab the div with ID of latestPriceSection, and filter to the USD value from https://www.monero.how/widget

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. You can't use HAP to scrape the information of this. Find a web service that can give you the information you need.

Alternatively you can use Selenium to get the HTML after the page has loaded the script. Or you the WebBrowser class, but that requires you to have a form application where the form contains the WebBrowser.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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