TypeError: dom.getElementsByTagName is not a function Node.js
TypeError: dom.getElementsByTagName is not a function Node.js
我刚开始使用 HTML(我不是网络开发人员)和网络。我正在尝试获取 html 文档中的标签列表,但我得到了 TypeError: dom.getElementsByTagName is not a function
。
我使用 axios
发出获取请求,然后使用 cheerio
将 response.data
加载到我调用 getElementsByTagName
方法的 html 文档中。
我是否缺少加载某些模块或我做错了什么??
一如既往,非常感谢您的帮助。
这是我的方法:
const db = require("../config/database");
const axios = require('axios');
const jsdom = require('jsdom');
const{JSDOM} = jsdom;
const cheerio = require('cheerio');
// const Route = require('../schemas/route');
const apiKey = process.env.API_KEY;
exports.ingest = async(req, res) => {
const {url} = req.query;
const response = await axios.get(url); // Property 'get' does not exist on type 'typeof import("/Volumes/ProjectsSSD/FixitServer/fixit_server_node/node_modules/axios/index")' but it does makje the request..??
// console.log('response is: ', response);
if (response.status == 200){
const document = cheerio.load(response.data, {decodeEntities: true}, false).html();
// const document = cheerio.load(response.data, {decodeEntities: false}, true);
console.log(' #################### response.data is: \n\n ', document);
// var tag = document.createElement("div"); // TypeError: document.createElement is not a function
// tag.innerHTML = str;
// var t = tag.getElementsByTagName("*");
// var ele = [];
// for (var i = 0; i < t.length; i++) {
// ele.push(t[i].tagName);
// }
// console.log('tags are: ', ele);
const dom = new JSDOM(document);
console.log(' @@@@@@@@@@@@@@@@@@@@ dom is: \n\n', dom);
const tags = document.getElementsByTagName('*'); // TypeError: dom.getElementsByTagName is not a function
console.log('tags are: ', tags);
} else{
res.status(response.status).send(response.statusText);
}
};
dom.getElementsByTagName
应该是 dom.window.document.getElementsByTagName
.
我刚开始使用 HTML(我不是网络开发人员)和网络。我正在尝试获取 html 文档中的标签列表,但我得到了 TypeError: dom.getElementsByTagName is not a function
。
我使用 axios
发出获取请求,然后使用 cheerio
将 response.data
加载到我调用 getElementsByTagName
方法的 html 文档中。
我是否缺少加载某些模块或我做错了什么??
一如既往,非常感谢您的帮助。
这是我的方法:
const db = require("../config/database");
const axios = require('axios');
const jsdom = require('jsdom');
const{JSDOM} = jsdom;
const cheerio = require('cheerio');
// const Route = require('../schemas/route');
const apiKey = process.env.API_KEY;
exports.ingest = async(req, res) => {
const {url} = req.query;
const response = await axios.get(url); // Property 'get' does not exist on type 'typeof import("/Volumes/ProjectsSSD/FixitServer/fixit_server_node/node_modules/axios/index")' but it does makje the request..??
// console.log('response is: ', response);
if (response.status == 200){
const document = cheerio.load(response.data, {decodeEntities: true}, false).html();
// const document = cheerio.load(response.data, {decodeEntities: false}, true);
console.log(' #################### response.data is: \n\n ', document);
// var tag = document.createElement("div"); // TypeError: document.createElement is not a function
// tag.innerHTML = str;
// var t = tag.getElementsByTagName("*");
// var ele = [];
// for (var i = 0; i < t.length; i++) {
// ele.push(t[i].tagName);
// }
// console.log('tags are: ', ele);
const dom = new JSDOM(document);
console.log(' @@@@@@@@@@@@@@@@@@@@ dom is: \n\n', dom);
const tags = document.getElementsByTagName('*'); // TypeError: dom.getElementsByTagName is not a function
console.log('tags are: ', tags);
} else{
res.status(response.status).send(response.statusText);
}
};
dom.getElementsByTagName
应该是 dom.window.document.getElementsByTagName
.