所以我使用这个URL是因为我想使用axios和cheerio抓取html: https://www.mgewholesale.com/ecommerce/Bags%20and%20Cases.cfm?cat_id=876
我在 postman 中测试了一个 get 请求,它在状态 200 下工作正常
使用此代码也适用于状态 200,但 response.data 为空
更新,因此使用此代码我得到了填充数据对象的实际响应,但是当我尝试访问response.data时,它向我显示了此错误:
const axios = require('axios');
const cheerio = require('cheerio');
const https = require('https');
let fs = require('fs');
const httpsAgent = new https.Agent({ keepAlive: true });
axios
.get('https://www.mgewholesale.com/ecommerce/Bags%20and%20Cases.cfm', {
httpsAgent,
params: {
cat_id: '876',
},
headers: {
'Accept-Encoding': 'gzip, deflate, br',
},
//is the same as set the entire url
})
.then((res) => {
console.log(res.data)
//this triggers the error
// let status = res.status;
console.log(status);
//Status 200
console.log(response)
//This brings the entire response with data object filled
});
ERROR:
(node:9068) UnhandledPromiseRejectionWarning: Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)
(node:9068) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我尝试使用整个网址和带有参数的网址,它会给我带来空数据,但如果我尝试使用其他网址,例如: https://www.google.com它给我带来了实际的 html。
最佳答案
问题是您的查询参数未正确添加。
从 axios.get
的第二个参数中删除 + '.json'
。
令我惊讶的是,这本身并没有引发错误,但显然 axios 只是一起玩并附加 0=[object+Object].json
,将您的 URL 变成: https://www.mgewholesale.com/ecommerce/Bags%20and%20Cases.cfm?0=[object+Object].json
我无法向其他答案添加评论,但这是不正确的,因为您在调用 .get() 后正确使用了 promise 链(
. .then
)
编辑:
对于这个特定的 URL,您似乎需要一些额外的 header ,以及在初始响应后保持连接处于事件状态的能力:
const axios = require('axios'); //15k (gzipped: 5.1k)
const cheerio = require('cheerio');
const https = require('https');
let fs = require('fs');
const httpsAgent = new https.Agent({ keepAlive: true });
axios
.get('https://www.mgewholesale.com/ecommerce/Bags%20and%20Cases.cfm', {
httpsAgent,
params: {
cat_id: '876',
},
headers: {
'Accept-Encoding': 'gzip, deflate, br',
},
//is the same as set the entire url
})
.then((res) => {
let status = res.status;
console.log(status);
//This should now output the html content
console.log(res.data);
})
.catch(err => console.error(err));
编辑2:
在上面的代码中添加了处理错误的正确方法。
编辑3:
确保您在 .then()
block 中记录的变量均已定义。另外,要获得更多有用的错误,请将 .catch()
添加到末尾:
.then((res) => {
console.log(res.data);
//this triggers the error
let status = res.status;
console.log(status);
//Status 200
console.log(res);
//This brings the entire response with data object filled
})
.catch(err => console.error(err));
关于javascript - Axios url get html响应请求为200,但response.data为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61070239/