Cheerio Web Scrape:标签之间没有换行符
Cheerio Web Scape: No Line Break Between Tags
当我使用 $('.episode-title')
时,我得到的输出是
Otona no Bouguya-san S2 — 02Otona no Bouguya-san S2 — 01
我想要的输出是
Otona no Bouguya-san S2 — 02 \n Otona no Bouguya-san S2 — 01
我该怎么做?
编辑 1 我的代码
const Nightmare = require('nightmare');
const cheerio = require('cheerio');
const url = `https://subsplease.org/shows/otona-no-bouguya-san-s2/`
const nightmare = Nightmare({ show: true })
// Request making using nightmare
nightmare
.goto(url)
.wait('body')
.wait('label.episode-title')
.evaluate(() => document.querySelector('body').innerHTML)
.end()
.then(response => {
console.log(getData(response));
}).catch(err => {
console.log(err);
});
let getData = html => {
const $ = cheerio.load(html);
let title = $('.episode-title').text()
console.log(title)
}
编辑 2 JSON
{
"episodes": [
{
"title": "Otona no Bouguya-san S2 — 01",
"torrents": [
{
"resolution": "1080p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "720p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "sd",
"magnet": "url",
"torrent": "url"
}
]
},
{
"title": "Otona no Bouguya-san S2 — 02",
"torrents": [
{
"resolution": "1080p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "720p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "sd",
"magnet": "url",
"torrent": "url"
}
]
}
]
}
因为你有两个元素 class .episode-title
你应该使用 map
或 forEach
。这是一个:
let titles = $('.episode-title').map((i, el) => $(el).text()).get();
console.log(titles);
// => ["Otona no Bouguya-san S2 — 02", "Otona no Bouguya-san S2 — 01"]
第一个使用:
$('.episode-title').first().text()
当我使用 $('.episode-title')
时,我得到的输出是
Otona no Bouguya-san S2 — 02Otona no Bouguya-san S2 — 01
我想要的输出是
Otona no Bouguya-san S2 — 02 \n Otona no Bouguya-san S2 — 01
我该怎么做?
编辑 1 我的代码
const Nightmare = require('nightmare');
const cheerio = require('cheerio');
const url = `https://subsplease.org/shows/otona-no-bouguya-san-s2/`
const nightmare = Nightmare({ show: true })
// Request making using nightmare
nightmare
.goto(url)
.wait('body')
.wait('label.episode-title')
.evaluate(() => document.querySelector('body').innerHTML)
.end()
.then(response => {
console.log(getData(response));
}).catch(err => {
console.log(err);
});
let getData = html => {
const $ = cheerio.load(html);
let title = $('.episode-title').text()
console.log(title)
}
编辑 2 JSON
{
"episodes": [
{
"title": "Otona no Bouguya-san S2 — 01",
"torrents": [
{
"resolution": "1080p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "720p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "sd",
"magnet": "url",
"torrent": "url"
}
]
},
{
"title": "Otona no Bouguya-san S2 — 02",
"torrents": [
{
"resolution": "1080p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "720p",
"magnet": "url",
"torrent": "url"
},
{
"resolution": "sd",
"magnet": "url",
"torrent": "url"
}
]
}
]
}
因为你有两个元素 class .episode-title
你应该使用 map
或 forEach
。这是一个:
let titles = $('.episode-title').map((i, el) => $(el).text()).get();
console.log(titles);
// => ["Otona no Bouguya-san S2 — 02", "Otona no Bouguya-san S2 — 01"]
第一个使用:
$('.episode-title').first().text()