Javascript 如何从该响应中获取href?
我有一些代码,其中我得到div类的第一个子类的响应,我想从我的第一个变量得到993307。我怎么做Javascript 如何从该响应中获取href?,javascript,html,cheerio,request-promise,Javascript,Html,Cheerio,Request Promise,我有一些代码,其中我得到div类的第一个子类的响应,我想从我的第一个变量得到993307。我怎么做 <a href="https://osu.ppy.sh/beatmapsets/993307/discussion#/1050967" title="MIIRO (TV Size) - AKINO from bless4 (mapped by Sotarks)"> 这是第一个变量的响应 这就是你要找的吗 var href = $(first).find('a').first().at
<a href="https://osu.ppy.sh/beatmapsets/993307/discussion#/1050967" title="MIIRO (TV Size) - AKINO from bless4 (mapped by Sotarks)">
这是第一个变量的响应
这就是你要找的吗
var href = $(first).find('a').first().attr('href');
var matches = href.match(/\/(\d+)\//);
if (matches[1]) {
console.log(matches[1]);
}
假设您的响应是字符串。使用DomParser 让响应='被取消资格。原因:[无预览]。2019年7月3日下午3:17:20 UTC' var parser=新的DOMParser;//启动DomParser var data=parser.parseFromStringresponse,“text/html”; 让atagLink=data.querySelectora.getAttributehref//获取a标记的href属性
console.logatagLink.match/\d+/[0]//与regex匹配它是字符串吗?还是html节点?
<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 href = $(first).find('a').first().attr('href');
var matches = href.match(/\/(\d+)\//);
if (matches[1]) {
console.log(matches[1]);
}