网络刮痧同时满足条件

时间:2017-05-12 22:36:07

标签: javascript html node.js web-scraping tags

我正在抓取此网站的一些数据:https://masteroverwatch.com/profile/pc/us/calvin-1337,特别是span.summary-hero-name。这是我的代码:

scrapeIt("https://masteroverwatch.com/profile/pc/us/calvin-1337",  {
  title: "span.summary-hero-name"
}).then(page => {
  console.log(page.title)
});

这会返回它应该执行的McreeWidowmakerBastion,因为有3个带有类summary-hero-name的英雄,但我只想要源代码中的第一个(这是最常用的)。如果那是不可能的话,那么我希望满足href/profile/pc/us/Calvin-1337/heroes/6(最高者)的条件。

1 个答案:

答案 0 :(得分:0)

您似乎需要高级CSS选择器!

有人可能会考虑:first-child:first-of-type,但它无法正常工作......我已经分析了您分享的页面,我认为您需要以下选择器:

a[href$="/heroes/6"] + .row

我已经使用HTML代码的有趣部分复制了用例。红色div是您要选择的红色.row { padding: 20px; margin: 20px; border: solid 1px black; } a[href$="/heroes/6"] + .row { color: red; }。它包含您需要的所有信息。

<div class="data-heroes-summary widget">
  <div class="widget-title">Favorite Hero Performance</div>
    <div class="summary-list">
      <div class="summary-row-container">
        <a class="summary-row-link" href="/profile/pc/us/Calvin-1337/heroes/6"></a>
        <div class="row">
          <div class="summary-icon col-xs-5">
            <span class="summary-icon-unit" style="background-image:url('https://blzgdapipro-a.akamaihd.net/hero/mccree/hero-select-portrait.png');"></span>
            <strong>
              <span class="summary-hero-name">McCree</span>
              <div class="summary-hero-role">Offense</div>
            </strong>
           </div>
           <div class="summary-stats col-xs-4">
            <div class="summary-stats-kda stats-assists">
              <strong>3.44</strong>:1 K/D
            </div>
            <div class="summary-stats-kills">
              <span>7,443 / 2,166</span>
            </div>
          </div>
          <div class="summary-winrate col-xs-3">
            <strong class="stats-kills">58.5%</strong>
            <span>340 Games</span>
          </div>
        </div>
      </div>
      <div class="summary-row-container">
        <a class="summary-row-link" href="/profile/pc/us/Calvin-1337/heroes/9"></a>
        <div class="row">
           <div class="summary-icon col-xs-5">
             <span class="summary-icon-unit" style="background-image:url('https://blzgdapipro-a.akamaihd.net/hero/widowmaker/hero-select-portrait.png');"></span>
             <strong>
               <span class="summary-hero-name">Widowmaker</span>
               <div class="summary-hero-role">Defense</div>
             </strong>
           </div>
           <div class="summary-stats col-xs-4">
            <div class="summary-stats-kda stats-assists">
              <strong>4.29</strong>:1 K/D
            </div>
            <div class="summary-stats-kills">
              <span>6,827 / 1,590</span>
            </div>
          </div>
          <div class="summary-winrate col-xs-3">
            <strong class="stats-kills">64.6%</strong>
            <span>339 Games</span>
          </div>
        </div>
      </div>
      <div class="summary-row-container">
        <a class="summary-row-link" href="/profile/pc/us/Calvin-1337/heroes/15"></a>
        <div class="row">
          <div class="summary-icon col-xs-5">
            <span class="summary-icon-unit" style="background-image:url('https://blzgdapipro-a.akamaihd.net/hero/bastion/hero-select-portrait.png');"></span>
            <strong>
              <span class="summary-hero-name">Bastion</span>
              <div class="summary-hero-role">Defense</div>
            </strong>
          </div>
        <div class="summary-stats col-xs-4">
          <div class="summary-stats-kda stats-assists">
            <strong>3.01</strong>:1 K/D
          </div>
          <div class="summary-stats-kills">
            <span>810 / 269</span>
          </div>
        </div>
        <div class="summary-winrate col-xs-3">
          <strong class="stats-kills">61.4%</strong>
          <span>44 Games</span>
        </div>
      </div>
    </div>
  </div>
  <a href="/profile/pc/us/Calvin-1337/heroes" class="summary-more-link">View More</a>
</div>
__init__