如何从此响应中获取href?

时间:2019-07-03 18:49:12

标签: javascript html cheerio request-promise

我有一些代码,它可以获取div类的第一个孩子的响应,我想从我的993307变量中获取first。我该怎么办?

<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>

2 个答案:

答案 0 :(得分:0)

假设您的回复是一个字符串。使用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())
    })
  })

答案 1 :(得分:-1)

这是您要寻找的吗?

var href = $(first).find('a').first().attr('href');
var matches = href.match(/\/(\d+)\//);

if (matches[1]) {
    console.log(matches[1]);
}