我正在使用请求 nodejs 模块获取网站的 html,如下所示:
var request = require('request');
request("http://www.thenewschool.org/", function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("body>>>>>>>>>>");
} else {
console.log("error>>>>>>>>>"+error);
console.log("response statusCode>>>>>>>>>"+response.statusCode);
console.log("response body>>>>>>>>>"+response.body);
}
})
这给了我这个输出
error>>>>>>>>>null
response statusCode>>>>>>>>>403
response body>>>>>>>>>Sorry, this request has been blocked due to an invalid user agent.
这在大多数情况下都是通过的,但在这种情况下失败了,有人可以帮我解决这个问题吗。
最佳答案
您只需在 header 中传递 user-agent
(因为 URL 需要它),例如:
var options = {
headers: {'user-agent': 'node.js'}
}
request("http://www.thenewschool.org/", options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("body>>>>>>>>>>" + body);
} else {
console.log("error>>>>>>>>>"+error);
console.log("response statusCode>>>>>>>>>"+response.statusCode);
console.log("response body>>>>>>>>>"+response.body);
}
})
关于javascript - 请求 Node 模块不提供 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508971/