【发布时间】:2020-04-06 22:58:11
【问题描述】:
所以我使用这个 URL 是因为我想使用 axios 和 Cheerio 来抓取 html: https://www.mgewholesale.com/ecommerce/Bags%20and%20Cases.cfm?cat_id=876
我在邮递员中测试了一个获取请求,它在状态 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.
我尝试使用整个 url 和带有参数的 url,它给我带来了空数据,但如果我尝试使用其他 url,例如:https://www.google.com,它给我带来了实际的 html。
【问题讨论】:
标签: javascript node.js web-scraping axios cheerio