C#HTMLAgilityPack需要帮助创建xpath

时间:2016-12-12 07:30:12

标签: c# xpath

<html lang="en" class="gr__r6db_com">

<head>
  <meta charset="UTF-8">
  <title>Search results for kinglio&nbsp;| R6DB</title>
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
  <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
  <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#ff0000">
  <meta id="meta_desc" name="description" content="Search players named kinglio on R6DB">
  <meta name="keywords" content="r6db,gitgudscrub,rainbow 6, rainbow six siege, database">
  <meta name="apple-mobile-web-app-title" content="R6DB">
  <meta name="application-name" content="R6DB">
  <meta name="theme-color" content="#ffffff">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="/css/app.css">
</head>

<body data-gr-c-s-loaded="true">
  <div id="mount">
    <div class="app is-results">
      <div class="app-background">
        <img src="/assets/nippon.jpg" class="clear">
        <img src="/assets/nippon-blurred.jpg" class="blur">
      </div>
      <div class="app-page">
        <div class="search">
          <h1 class="title is-1 search-title">R6DB</h1>
          <div class="search-form">
            <div class="column is-small-8 search-input">
              <input type="text"><span><input type="checkbox" id="exactSearch"><label for="exactSearch">exact name</label></span>
            </div>
            <button class="search-submit">Search</button>
          </div>
          <div class="colums is-multiline search-results">
            <div class="playercard player-6881796a-9f51-4386-83f9-136ea45d0faa is-user is-visible">
              <a href="/player/6881796a-9f51-4386-83f9-136ea45d0faa" class="card-image">
                <img src="/assets/noavatar.png">
              </a>
              <div class="card-content">
                <div class="player-identification"><a href="/player/6881796a-9f51-4386-83f9-136ea45d0faa" class="player-name">Kinglio</a><span class="player-id">6881796a-9f51-4386-83f9-136ea45d0faa</span>
                </div>
                <div class="player-aliases">
                  <header>known aliases</header>
                  <ul>
                    <li>Kinglio</li>
                  </ul>
                </div><a href="https://game-rainbow6.ubi.com/en-gb/uplay/player-statistics/6881796a-9f51-4386-83f9-136ea45d0faa/multiplayer" class="player-uplaylink">› view on uplay</a>
              </div>
            </div>
            <div class="playercard player-344b8c89-7deb-4e0f-ad42-942ee314c072 is-user is-visible">
              <a href="/player/344b8c89-7deb-4e0f-ad42-942ee314c072" class="card-image">
                <img src="//uplay-avatars.s3.amazonaws.com/344b8c89-7deb-4e0f-ad42-942ee314c072/default_146_146.png">
              </a>
              <div class="card-content">
                <div class="player-identification"><a href="/player/344b8c89-7deb-4e0f-ad42-942ee314c072" class="player-name">KingLio.-</a><span class="player-id">344b8c89-7deb-4e0f-ad42-942ee314c072</span>
                </div>
                <div class="player-aliases">
                  <header>known aliases</header>
                  <ul>
                    <li>KingLio.-</li>
                    <li>KingLio.TG</li>
                    <li>and 7more</li>
                  </ul>
                </div><a href="https://game-rainbow6.ubi.com/en-gb/uplay/player-statistics/344b8c89-7deb-4e0f-ad42-942ee314c072/multiplayer" class="player-uplaylink">› view on uplay</a>
              </div>
            </div>
            <div class="playercard player-922eec9c-61b6-4a09-892e-96ae2be3218d is-user is-visible">
              <a href="/player/922eec9c-61b6-4a09-892e-96ae2be3218d" class="card-image">
                <img src="//uplay-avatars.s3.amazonaws.com/922eec9c-61b6-4a09-892e-96ae2be3218d/default_146_146.png">
              </a>
              <div class="card-content">
                <div class="player-identification"><a href="/player/922eec9c-61b6-4a09-892e-96ae2be3218d" class="player-name">PS4Gamer.--</a><span class="player-id">922eec9c-61b6-4a09-892e-96ae2be3218d</span>
                </div>
                <div class="player-aliases">
                  <header>known aliases</header>
                  <ul>
                    <li>PS4Gamer.--</li>
                    <li>KingLio.1L</li>
                    <li>and 2more</li>
                  </ul>
                </div><a href="https://game-rainbow6.ubi.com/en-gb/uplay/player-statistics/922eec9c-61b6-4a09-892e-96ae2be3218d/multiplayer" class="player-uplaylink">› view on uplay</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script async="" src="https://www.google-analytics.com/analytics.js"></script>
  <script src="/js/app.js"></script>


  <iframe src="chrome-extension://hgimnogjllphhhkhlmebbmlgjoejdpjl/bar.html" id="xh-bar" class="hidden"></iframe>
  <img id="hzDownscaled" style="position: absolute; top: -10000px;">
</body>

</html>

我正在尝试从网站上抓取一个唯一的ID,具体取决于用户输入文本框的内容。但是,无论我尝试什么,我都无法使用正确的Xpath。

这是我最近的尝试

            string searchUsernameUrl = "https://www.r6db.com/search/" + txtUsername.Text;

        HtmlWeb searchWeb = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument searchDoc = searchWeb.Load(searchUsernameUrl);


        try
        {
            txtUplayID.Text = searchDoc.DocumentNode.SelectNodes("//div[@class='colums is-multiline search-results']/div[1]/div/div[1]/span")[0].InnerText;
        }
        catch (NullReferenceException)
        { 
            MessageBox.Show("Unable to find username ID.", "Error");
        }

这是该网站的HTML代码。搜索时,可能会出现任意数量的结果。在这个例子中有3个。我只想复制第一个例子中的代码。

Here is a picture of the HTML from the website upon searching, showing what I need an xpath for

1 个答案:

答案 0 :(得分:0)

如果没有更多具有class player-id的span元素,那么你可以使用它:

var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//span[@class='player-id']");
Console.WriteLine(htmlNodes.First().InnerText);

或者如果还有更多,您可以使用Linq的Skip扩展方法或ElementAt方法通过索引获取所需的html节点...

或者只是foreach遍历htmlNodes,这个xpath表达式将返回所有的player-id

这是一个.net小提琴,它在你的html上演示了这个xpath表达式: https://dotnetfiddle.net/bZd4sM