你有没有使用过cheerio来解析html。有时太快会不确定?
我检查了返回未定义的页面。获取数据没有问题。因为我有条件检查来自cheerio的解析是否未定义。它将返回cheerio解析的html。它存在。但是cheerio parse 有时会返回undefined。如果我再次重复,有时它会返回未定义的解析?究竟是什么问题?
import fetch from 'node-fetch';
import { load } from 'cheerio';
(async () => {
const options = {
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
}
};
const response = await fetch('https://hentairead.com/page/1/', options);
const $ = load(await response.text());
const totalPage = $('span.pages')
.text()
.match(/\d{3,}/g)
.toString();
for(let page = 1; page <= totalPage; page++)
{
const response = await fetch(`https://hentairead.com/page/${page}/`, options);
const $ = load(await response.text());
$('div.item-thumb.c-image-hover > a').each(async (index, item) => {
const title = item.attribs.title;
const url = item.attribs.href;
const response = await fetch(`${url}english/`, options);
const html = await response.text();
const $ = load(html);
const totalPage = $('select#single-pager > option:last-child').attr('value');
if(totalPage !== undefined){
console.log(title);
console.log(totalPage);
}else{
console.log(html);
}
});
}
})();