无法使用 cheerio 读取未定义(读取 'statusCode')Nodejs 的属性
Cannot read properties of undefined (reading 'statusCode') Nodejs using cheerio
我正在尝试获取这个有趣请求的网页标题,那里的 cheerio 正面临这个问题无法读取未定义的属性(读取 'statusCode') 但是当我 运行 尝试 运行 单独使用静态值时它有效
任何人都可以帮助识别我在代码中所做的错误吗?
这是我的代码
var request = require("request");
var cheerio = require("cheerio");
jsonData = [
{ Domain: "bar-n-ranch.com" },
{ Domain: "barcelona-enabled.com" },
{ Domain: "barefootamelia.com" },
{ Domain: "barmranch.com" },
{ Domain: "barnstablepatriot.com" },
{ Domain: "barrieapartmentrentalsonline.com" },
{ Domain: "basquehomes.com" },
{ Domain: "bassmaster.com" },
{ Domain: "basswoodresort.com" },
];
function fetchTitle(url, onComplete = null) {
request(url, function (error, response, body) {
var output = url; // default to URL
if (!error && response.statusCode === 200) {
var $ = cheerio.load(body);
console.log(`URL = ${url}`);
var title = $("head > title").text().trim();
console.log(`Title = ${title}`);
output = `[${title}] (${url})`;
} else {
console.log(`Error = ${error}, code = ${response.statusCode}`);
}
console.log(`output = ${output} \n\n`);
if (onComplete) onComplete(output);
});
}
jsonData.forEach(function (table) {
var tableName = table.Domain;
var URL = "http://" + tableName;
fetchTitle(URL);
});
当我传递像 fetchtitle("https://www.googlecom") 这样的值时,它可以工作,但是当我尝试循环 JSON data
时出现错误
错误堆栈跟踪
^
TypeError: Cannot read properties of undefined (reading 'statusCode')
at Request._callback (C:\naveen\Project\Final\scrap example\test.js:29:56)
at self.callback (C:\naveen\Project\Final\scrap example\node_modules\request\request.js:185:22)
at Request.emit (node:events:390:28)
at Request.onRequestError (C:\naveen\Project\Final\scrap example\node_modules\request\request.js:877:8)
at ClientRequest.emit (node:events:390:28)
at Socket.socketErrorListener (node:_http_client:447:9)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
非常感谢您
有时当您在服务器上循环多个请求时,套接字可能会挂起(忙于其他请求)或服务器无法对请求进行排队,这可能导致请求无响应。对此的最佳解决方案是,您必须先检查来自请求的响应对象,然后再访问它的属性。
if (!error && (response && response.statusCode) === 200) {
var $ = cheerio.load(body);
console.log(`URL = ${url}`);
var title = $("head > title").text().trim();
console.log(`Title = ${title}`);
output = `[${title}] (${url})`;
}
else {
console.log(`Error = ${error}, code = ${response && response.statusCode}`);
}
我正在尝试获取这个有趣请求的网页标题,那里的 cheerio 正面临这个问题无法读取未定义的属性(读取 'statusCode') 但是当我 运行 尝试 运行 单独使用静态值时它有效
任何人都可以帮助识别我在代码中所做的错误吗?
这是我的代码
var request = require("request");
var cheerio = require("cheerio");
jsonData = [
{ Domain: "bar-n-ranch.com" },
{ Domain: "barcelona-enabled.com" },
{ Domain: "barefootamelia.com" },
{ Domain: "barmranch.com" },
{ Domain: "barnstablepatriot.com" },
{ Domain: "barrieapartmentrentalsonline.com" },
{ Domain: "basquehomes.com" },
{ Domain: "bassmaster.com" },
{ Domain: "basswoodresort.com" },
];
function fetchTitle(url, onComplete = null) {
request(url, function (error, response, body) {
var output = url; // default to URL
if (!error && response.statusCode === 200) {
var $ = cheerio.load(body);
console.log(`URL = ${url}`);
var title = $("head > title").text().trim();
console.log(`Title = ${title}`);
output = `[${title}] (${url})`;
} else {
console.log(`Error = ${error}, code = ${response.statusCode}`);
}
console.log(`output = ${output} \n\n`);
if (onComplete) onComplete(output);
});
}
jsonData.forEach(function (table) {
var tableName = table.Domain;
var URL = "http://" + tableName;
fetchTitle(URL);
});
当我传递像 fetchtitle("https://www.googlecom") 这样的值时,它可以工作,但是当我尝试循环 JSON data
时出现错误错误堆栈跟踪 ^
TypeError: Cannot read properties of undefined (reading 'statusCode')
at Request._callback (C:\naveen\Project\Final\scrap example\test.js:29:56)
at self.callback (C:\naveen\Project\Final\scrap example\node_modules\request\request.js:185:22)
at Request.emit (node:events:390:28)
at Request.onRequestError (C:\naveen\Project\Final\scrap example\node_modules\request\request.js:877:8)
at ClientRequest.emit (node:events:390:28)
at Socket.socketErrorListener (node:_http_client:447:9)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
非常感谢您
有时当您在服务器上循环多个请求时,套接字可能会挂起(忙于其他请求)或服务器无法对请求进行排队,这可能导致请求无响应。对此的最佳解决方案是,您必须先检查来自请求的响应对象,然后再访问它的属性。
if (!error && (response && response.statusCode) === 200) {
var $ = cheerio.load(body);
console.log(`URL = ${url}`);
var title = $("head > title").text().trim();
console.log(`Title = ${title}`);
output = `[${title}] (${url})`;
}
else {
console.log(`Error = ${error}, code = ${response && response.statusCode}`);
}