0

我有一段 html(这里只展示了其中的一部分),如下所示:

<div class='games'>
    Won <span class="text-success">37</span>,
    lost <span class="text-error">51</span>,
    ratio <span>42.05</span>%
</div>
<div class='race'>
    Favorite Race:
    <div class='race-terran races size-16'></div>
    <span>Terran</span>
</div>
</div>
<div class='span3 ranks'>
    <dl class='dl-horizontal'>
        <dt>
            <div class='flag-global flags size-16'></div>
            World Rank:
        </dt>
        <dd>
            #<span>6,654</span>
            of
            <span>58,431</span>
        </dd>
        <dt>
            <div class='flag-eu flags size-16'></div>
            Region Rank:
        </dt>
        <dd>
            #<span>2,679</span>
            of
            <span>23,944</span>
        </dd>
        <dt>
            Bonus Pool:
        </dt>
        <dd>
            9
        </dd>
    </dl>
</div>
<div class='span3 ext-stats'>
    <dl class='dl-horizontal'>
        <dt class='wide'>
            <a href="/team/eu/11001756782/stimphano" class="alt">View Team Page</a>
        </dt>
        <dd></dd>
        <dt>Last Game:</dt>
        <dd>19 hours ago</dd>
        <dt>Last Update:</dt>
        <dd>6 hours ago</dd>
    </dl>
</div>

我必须阅读诸如“赢、输、比率”之类的内容以及其他内容。使用以下代码可以轻松访问前三个项目:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(_sc2RanksString);
HtmlAgilityPack.HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//span");

var won = doc.DocumentNode.SelectSingleNode("//div[@class='games']/*[@class='text-success']").InnerText;
var lost = doc.DocumentNode.SelectSingleNode("//div[@class='games']/*[@class='text-error']").InnerText;
var points = doc.DocumentNode.SelectSingleNode("//div[@class='points']/*[@class='gold']").InnerText;
var bonusPool =
    doc.DocumentNode.SelectSingleNode("//div[@class='flag-eu flags size-16']//span//span/dt").InnerText;

但在上面的例子中,las 行我无法获得“Bonus Pool”......似乎我为它提供了错误的 xpath 查询。

谁能告诉我如何达到奖金池和 Y 的地区排名 X

此外,我还需要获得最后一场比赛和最后一次更新。但是我真的很难在节点中找到东西。

4

1 回答 1

1
var bonusPool = doc.DocumentNode
                .SelectNodes("//div[@class='span3 ranks']/dl[@class='dl-horizontal']//dd")
                .Last()
                .InnerText.Trim();

var regionRank = doc.DocumentNode
                .SelectNodes("//div[@class='span3 ranks']/dl[@class='dl-horizontal']/dd[2]/span")
                .Select(x => x.InnerText)
                .ToArray();


var lastGame = doc.DocumentNode
                .SelectSingleNode("//div[@class='span3 ext-stats']/dl[@class='dl-horizontal']/dd[2]")
                .InnerText;

var lastUpdate = doc.DocumentNode
                .SelectSingleNode("//div[@class='span3 ext-stats']/dl[@class='dl-horizontal']/dd[3]")
                .InnerText;
于 2013-09-22T20:00:25.527 回答