【问题标题】:How I can parse HTML table content in Discord Chat? Discord.NET C#如何在 Discord Chat 中解析 HTML 表格内容? Discord.NET C#
【发布时间】:2017-05-11 10:33:31
【问题描述】:

美好的一天, 我想解析网站表中的内容。 在网站上有一个顶级每周经验玩家排名。并且使用命令 ~weekly 我想击败最好的 20 名球员。 现在我有以下代码:

commands.CreateCommand("weekly")
            .Do(async (e) =>
            {
                WebClient webClient = new WebClient();
                string html = webClient.DownloadString("http://combatarms.nexon.net/de/ranking/player");

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(html);

                foreach (var cell in doc.DocumentNode.SelectNodes("//table[@class='ranking_tbl']/tr/td"))
                {
                    await e.Channel.SendMessage(cell.InnerText);
                }

               // await e.Channel.SendMessage("test"); 
            });

但它没有向我显示任何东西,为什么我错了? 更好的事情是我可以做一个数组(以前有但没用),我可以说“我只想要第一个<tr>(#),第二个<tr>(名字)和例如第 7 个<tr>(氏族名称)。

但是我失败了数组+将这些 tr 内容解析为不和谐:/

例如表中的 1 行是:

<table class="ranking_tbl" summary="">
            <colgroup>
                <col width="80">
                <col width="250">
                <col width="100">
                <col width="150">
                <col width="100">
                <col width="100">
                <col width="280">
            </colgroup>
            <thead>
                <tr>
                    <th></th>
                    <th>Name </th>
                    <th>Rang </th>
                    <th>EP </th>
                    <th>KDR </th>
                    <th>Land </th>
                    <th>Clan- </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="cell_left">1</td>
                    <td><a href="/de/profile/player/RADICALIST">RADICALIST</a></td>
                    <td><img src="http://caimage.nexoneu.com/Rank/rank_51.gif" alt=""></td>
                    <td>5.219.130</td>
                    <td>1,46</td>
                    <td><img src="http://caimage.nexoneu.com/Web_site/Main/img/flag/SI.png" alt=""></td>
                    <td><a href="/de/clan/profile/Jasmine%20Thompson">Jasmine Thompson</a></td>
                </tr>

【问题讨论】:

  • 下载的文本不包含 值。看一下 html 变量的内容,你会看到 .
  • 有td属性:/
  • 但是它们是空的!
  • 但是为什么呢?正如您在 html 代码中看到的,td 不是空的
  • 我不知道为什么。你只需要找到下载正确内容的方法,然后你就可以想到如何解析html并获得你想要的。

标签: c# html parsing discord discord.net


【解决方案1】:

我认为表格中的内容是动态生成的,页面中的一些javascript代码会生成它。但是这个动态内容是在文档加载之后加载的。 因此,当您下载页面时,您无法获取所有内容。
你可以在这里阅读更多信息:
htmlagilitypack and dynamic content issue
webclient doesn't download the web page completely
Load dynamically generated HTML Code in WebClient
How to extract dynamic ajax content from a web page
Scraping data dynamically generated by JavaScript in html document using C#

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 2016-09-20
    • 2013-05-10
    • 2015-10-09
    • 2014-10-18
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    相关资源
    最近更新 更多