node.js - Cheerio 有时会返回 undefined
问题描述
你是否曾经使用cheerio 来解析html。有时如果太快它会不确定?
我检查了返回未定义的页面。获取数据没有问题。因为我有条件检查来自cheerio的解析是否未定义。它将返回cheerio 解析的html。它存在。但是cheerio parse有时会返回未定义。如果我再次重复,有时它会返回未定义的解析?究竟是什么问题?
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);
}
});
}
})();
解决方案
推荐阅读
- meeting-request - Google Meet:如何获取网址?
- javascript - Tempalte 文件不会在浏览器中更新
- vaadin - Vaadin 14:交换组合框下拉图标
- reactjs - React typescript - 类型必须有一个返回迭代器的“[Symbol.iterator]()”方法
- python - python - 如何在python中保存一个特定列的数据类型必须为int32的csv文件?
- xamarin - 带有 Material Visual 的条目集中在 Xamarin Forms Android 中时的灰线
- gams-math - 如何加快 GAMS 中的模型生成过程
- vue.js - 仅在按下键时处理 VueJS 事件
- bert-language-model - 无法在 python 中导入 simpletransformers.ner,因为它说 ImportError: cannot import name 'BertweetTokenizer'
- laravel - 如何在子域 hostgator 上部署我的 laravel 项目