我正在研究使用Puppeteer和Cheerio实现的Web抓取节点服务器。
我有一个前端React应用程序使用Soundcloud-widget播放音乐,但事实是该API仅适用于正确的Soundcloud URL。
因此,我正在输入UI,以便通过Scraper在SoundCloud搜索上发送请求,并从那里获取结果。
$(a.soundTitle__title)
将名称保留在子span标记中(在他的“ class”属性中),并将URL作为href足够。
问题是我要得到
{
options: {
withDomLvl1: true,normalizeWhitespace: false,xml: false,decodeEntities: true
},length: 0,prevObject: {
options: {
withDomLvl1: true,decodeEntities: true
}
}
}
是跨度/歌曲名称的默认cheerio输出, 和href网址的“未定义”。
即使我尝试使用{.text()/ .attr(“ class”)/ etc ...}, undefind /此默认响应/错误text()不是函数
这是我的代码-
... req
const addaptReq = req.text.replace(' ','%20');
const url = `https://soundcloud.com/search?q=${addaptReq}`;
let myBrowser;
puppeteer
.launch()
.then(browser => {
myBrowser = browser;
return myBrowser.newPage();
})
.then(page => {
return page.goto(url).then(() => {
return page.content();
});
})
.then(html => {
// console.log(html)
$('a.soundTitle__title',html,).each(result => {
let songName = $('span',result)
let songURL = $(result).attr('href')
// hopefuly name will give the span text (which is it class and href the URL)
console.log(songName,songURL)
})
}).then(() => {
myBrowser.close()
})
.catch(err => {
console.log(err);
});
我用devtools附加了soundcloud的屏幕截图,也许我在那儿做错了什么?
提前谢谢!