Nodejs cheerio 拿走所有的文字
Nodejs cheerio take all the texts
我有以下 html 页面代码:
<div class="textwidget">
<p>
<strong>Monday-Tuesday (Feb 21-22)</strong>
<br />
<a href="https://dramaday.net/moonshine/">Moonshine</a> 15-16 (KBS2)<br />
<a href="https://dramaday.net/ghost-doctor/">Ghost Doctor</a> 15-16 (tvN)<br />
<strong>Wednesday-Thursday (Feb 23-24)</strong>
<br />
<a href="https://dramaday.net/rookie-cops/">Rookie Cops</a> 9-10
(Disney+)<br />
<a href="https://dramaday.net/grid/">Grid</a> 2 (Disney+)<br />
<a href="https://dramaday.net/thirty-nine/">Thirty Nine</a> 3-4 (JTBC)<br />
Kill Heel 1-2 (tvN)<br /> <!-- It is not taken -->
Sponsor 1-2 (MBN)<br /> <!-- It is not taken -->
<strong>Friday-Sunday (Feb 25-27)</strong>
<br />
Juvenile Justice 1-10 (Netflix)<br /> <!-- It is not taken -->
<a href="https://dramaday.net/through-the-darkness/"
>Through the Darkness</a
>
7-8 (SBS)<br />
<a href="https://dramaday.net/twenty-five-twenty-one/"
>Twenty Five Twenty One</a
>
5-6 (tvN)<br />
<a href="https://dramaday.net/forecasting-love-and-weather/"
>Forecasting Love and Weather</a
>
5-6 (JTBC)<br />
<a href="https://dramaday.net/young-lady-and-gentleman/"
>Young Lady and Gentleman</a
>
43-44 (KBS2)<br />
<a href="https://dramaday.net/love-ft-marriage-and-divorce/"
>Love (ft. Marriage and Divorce) S3</a
>
1-2 (CSTV)
</p>
</div>
我有以下js代码从页面获取信息:
async function getData() {
let element = [];
try {
const { data } = await axios.get("https://dramaday.net");
const $ = cheerio.load(data);
let day = 0;
let date = ""
$("div.textwidget > p >").each((_idx, el) => {
const text = $(el).text();
if(text.includes("Monday-Tuesday")){
day = 0;
date = text.replace('(','').replace(')','').split(' ')
} else if(text.includes("Wednesday-Thursday")){
day = 1;
date = text.replace('(','').replace(')','').split(' ')
} else if(text.includes("Friday-Sunday")){
day = 2;
date = text.replace('(','').replace(')','').split(' ')
} else {
const obj = {
name: text,
day,
date
}
element.push(obj);
}
});
return element;
} catch (error) {
throw error;
}
}
问题是不包含标签的文本不会被采用。
请参阅 html 代码中的示例,其中显示“未使用”。
我得到的结果是这样的,你可以看到有很多空名,我认为这是由于 br
标签造成的。
我可以通过将 else
替换为条件 text! == ''
的 else if
来解决它,但我没有解决文本未包含在任何标记中的问题。
除了我无法在 a
标签之后获取剧集编号和频道的文本之外。
你能帮帮我吗?
[
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{
name: 'Moonshine',
day: 0,
date: [ 'Monday-Tuesday', 'Feb', '21-22' ]
},
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{
name: 'Ghost Doctor',
day: 0,
date: [ 'Monday-Tuesday', 'Feb', '21-22' ]
},
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Rookie Cops',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Grid',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Thirty Nine',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Through the Darkness',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Twenty Five Twenty One',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Forecasting Love and Weather',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Young Lady and Gentleman',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Love (ft. Marriage and Divorce) S3',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
}
]
var element = [];
var day = -1, date = '', name = '';
$("div.textwidget > p").html().split('<br>').forEach(function (row) {
var el = $('<div>').append(row);
var text = el.text();
text = text.replace(/^\s+|\s+$/g, ''); // trim
text = text.replace(/\s+/g, ' ');
if (el.find('strong').size()) {
day += 1;
date = text.replace('(','').replace(')','').split(' ');
return;
}
name = text;
element.push({
name,
day,
date
});
})
console.log(element);
我有以下 html 页面代码:
<div class="textwidget">
<p>
<strong>Monday-Tuesday (Feb 21-22)</strong>
<br />
<a href="https://dramaday.net/moonshine/">Moonshine</a> 15-16 (KBS2)<br />
<a href="https://dramaday.net/ghost-doctor/">Ghost Doctor</a> 15-16 (tvN)<br />
<strong>Wednesday-Thursday (Feb 23-24)</strong>
<br />
<a href="https://dramaday.net/rookie-cops/">Rookie Cops</a> 9-10
(Disney+)<br />
<a href="https://dramaday.net/grid/">Grid</a> 2 (Disney+)<br />
<a href="https://dramaday.net/thirty-nine/">Thirty Nine</a> 3-4 (JTBC)<br />
Kill Heel 1-2 (tvN)<br /> <!-- It is not taken -->
Sponsor 1-2 (MBN)<br /> <!-- It is not taken -->
<strong>Friday-Sunday (Feb 25-27)</strong>
<br />
Juvenile Justice 1-10 (Netflix)<br /> <!-- It is not taken -->
<a href="https://dramaday.net/through-the-darkness/"
>Through the Darkness</a
>
7-8 (SBS)<br />
<a href="https://dramaday.net/twenty-five-twenty-one/"
>Twenty Five Twenty One</a
>
5-6 (tvN)<br />
<a href="https://dramaday.net/forecasting-love-and-weather/"
>Forecasting Love and Weather</a
>
5-6 (JTBC)<br />
<a href="https://dramaday.net/young-lady-and-gentleman/"
>Young Lady and Gentleman</a
>
43-44 (KBS2)<br />
<a href="https://dramaday.net/love-ft-marriage-and-divorce/"
>Love (ft. Marriage and Divorce) S3</a
>
1-2 (CSTV)
</p>
</div>
我有以下js代码从页面获取信息:
async function getData() {
let element = [];
try {
const { data } = await axios.get("https://dramaday.net");
const $ = cheerio.load(data);
let day = 0;
let date = ""
$("div.textwidget > p >").each((_idx, el) => {
const text = $(el).text();
if(text.includes("Monday-Tuesday")){
day = 0;
date = text.replace('(','').replace(')','').split(' ')
} else if(text.includes("Wednesday-Thursday")){
day = 1;
date = text.replace('(','').replace(')','').split(' ')
} else if(text.includes("Friday-Sunday")){
day = 2;
date = text.replace('(','').replace(')','').split(' ')
} else {
const obj = {
name: text,
day,
date
}
element.push(obj);
}
});
return element;
} catch (error) {
throw error;
}
}
问题是不包含标签的文本不会被采用。
请参阅 html 代码中的示例,其中显示“未使用”。
我得到的结果是这样的,你可以看到有很多空名,我认为这是由于 br
标签造成的。
我可以通过将 else
替换为条件 text! == ''
的 else if
来解决它,但我没有解决文本未包含在任何标记中的问题。
除了我无法在 a
标签之后获取剧集编号和频道的文本之外。
你能帮帮我吗?
[
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{
name: 'Moonshine',
day: 0,
date: [ 'Monday-Tuesday', 'Feb', '21-22' ]
},
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{
name: 'Ghost Doctor',
day: 0,
date: [ 'Monday-Tuesday', 'Feb', '21-22' ]
},
{ name: '', day: 0, date: [ 'Monday-Tuesday', 'Feb', '21-22' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Rookie Cops',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Grid',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{
name: 'Thirty Nine',
day: 1,
date: [ 'Wednesday-Thursday', 'Feb', '23-24' ]
},
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 1, date: [ 'Wednesday-Thursday', 'Feb', '23-24' ] },
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Through the Darkness',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Twenty Five Twenty One',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Forecasting Love and Weather',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Young Lady and Gentleman',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
},
{ name: '', day: 2, date: [ 'Friday-Sunday', 'Feb', '25-27' ] },
{
name: 'Love (ft. Marriage and Divorce) S3',
day: 2,
date: [ 'Friday-Sunday', 'Feb', '25-27' ]
}
]
var element = [];
var day = -1, date = '', name = '';
$("div.textwidget > p").html().split('<br>').forEach(function (row) {
var el = $('<div>').append(row);
var text = el.text();
text = text.replace(/^\s+|\s+$/g, ''); // trim
text = text.replace(/\s+/g, ' ');
if (el.find('strong').size()) {
day += 1;
date = text.replace('(','').replace(')','').split(' ');
return;
}
name = text;
element.push({
name,
day,
date
});
})
console.log(element);