【问题标题】:How to get href from this response?如何从此响应中获取href?
【发布时间】:2019-07-03 18:49:12
【问题描述】:

我有一些代码,我得到了 div 类的第一个孩子的响应,我想从我的 first 变量中获取 993307。我该怎么做?

<a href="https://osu.ppy.sh/beatmapsets/993307/discussion#/1050967" title="MIIRO (TV Size) - AKINO from bless4 (mapped by Sotarks)">
const cheerio = require('cheerio');
const rp = require('request-promise');

var array = [];

rp(`https://osu.ppy.sh/beatmapsets/events?user=&types%5B%5D=disqualify&min_date=&max_date=`)
  .then((html) => {
    let $ = cheerio.load(html);
    $('div#events.beatmapset-events').each(function(i, element) {
      var first = $(this).children().eq(1);
      console.log(first.html())
    })
  })
  .catch(console.error.bind(console));

这是来自first变量的回复

<div class="beatmapset-event">
  <a href="https://osu.ppy.sh/beatmapsets/993307/discussion#/1050967" title="
                   MIIRO (TV Size) - AKINO from bless4
                   (mapped by Sotarks)
               ">
    <img class="beatmapset-activities__beatmapset-cover" src="https://assets.ppy.sh/beatmaps/993307/covers/list.jpg?1562167122" srcset="https://assets.ppy.sh/beatmaps/993307/covers/list.jpg?156216712
2 1x, https://assets.ppy.sh/beatmaps/993307/covers/list@2x.jpg?1562167122 2x">
  </a>
  <div class="beatmapset-event__icon beatmapset-event__icon--disqualify beatmapset-activities__event-icon-spacer"></div>
  <div>
    <div class="beatmapset-event__content">
      Disqualified by <a class="user-name js-usercard" data-user-id="3388410" href="https://osu.ppy.sh/users/3388410" style="color: #6B3FA0">eiri-</a>. Reason: <a href="https://osu.ppy.sh/beatmapsets/9
93307/discussion#/1050967">#1050967</a> ([no preview]).
    </div>
    <div><time class="timeago" datetime="2019-07-03T15:17:20+00:00">July 3, 2019 at 3:17:20 PM UTC</time></div>
  </div>
</div>

【问题讨论】:

  • 是字符串吗?还是一个 html 节点?

标签: javascript html cheerio request-promise


【解决方案1】:

假设您的响应是一个字符串。使用DomParser()

let response = '<div class="beatmapset-event"> <a href="https://osu.ppy.sh/beatmapsets/993307/discussion#/1050967" title=" MIIRO (TV Size) - AKINO from bless4 (mapped by Sotarks) "> <img class="beatmapset-activities__beatmapset-cover" src="https://assets.ppy.sh/beatmaps/993307/covers/list.jpg?1562167122" srcset="https://assets.ppy.sh/beatmaps/993307/covers/list.jpg?156216712 2 1x, https://assets.ppy.sh/beatmaps/993307/covers/list@2x.jpg?1562167122 2x"> </a><div class="beatmapset-event__icon beatmapset-event__icon--disqualify beatmapset-activities__event-icon-spacer"></div><div><div class="beatmapset-event__content"> Disqualified by <a class="user-name js-usercard" data-user-id="3388410" href="https://osu.ppy.sh/users/3388410" style="color: #6B3FA0">eiri-</a>. Reason: <a href="https://osu.ppy.sh/beatmapsets/9 93307/discussion#/1050967">#1050967</a> ([no preview]).</div><div><time class="timeago" datetime="2019-07-03T15:17:20+00:00">July 3, 2019 at 3:17:20 PM UTC</time></div></div></div>'
var parser = new DOMParser(); // initiate DomParser()
var data = parser.parseFromString(response, 'text/html'); 
let atagLink = data.querySelector("a").getAttribute("href") // get the a tag's href attribute
console.log(atagLink.match(/(\d+)/)[0]) // match with regex

正则表达式详细信息(\d+) 匹配第一个出现的数字。

这就是你的代码

rp(`https://osu.ppy.sh/beatmapsets/events?user=&types%5B%5D=disqualify&min_date=&max_date=`)
  .then((html) => {
    let $ = cheerio.load(html);
    var parser = new DOMParser(); // initiate DomParser()
    var data = parser.parseFromString(html, 'text/html'); 
    let atagLink = data.querySelector("a").getAttribute("href") // get the a tag's href attribute
    let number = atagLink.match(/(\d+)/)[0]
    $('div#events.beatmapset-events').each(function(i, element) {
      var first = $(this).children().eq(1);
      console.log(first.html())
    })
  })

【讨论】:

    【解决方案2】:

    这是你要找的吗?

    var href = $(first).find('a').first().attr('href');
    var matches = href.match(/\/(\d+)\//);
    
    if (matches[1]) {
        console.log(matches[1]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-27
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      • 2021-10-07
      相关资源
      最近更新 更多