【发布时间】:2015-08-12 07:00:33
【问题描述】:
我正在使用请求 nodejs 模块来获取网站的 html,但不适用于以下重定向网站:
var request = require('request');
var options = {
url: "http://www.amwasia.com",
headers: {'user-agent': 'node.js'},
rejectUnauthorized: false,
followAllRedirects: true
};
request(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);
}
});
这给了我这个输出
正文>>>>>>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.amwmotors.com/">
</head>
<body>
</body>
</html>
这不是准确的 html。
这个网站正在重定向到这个页面http://www.amwmotors.com/
另一个例子是网站:http://www.pmat.or.th 并重定向到页面http://www.pmat.or.th/main/
这也没有给出正确的输出。
我也尝试过在选项中不使用 followAllRedirects: true。
请帮忙?
【问题讨论】:
-
当我 curl amwasia.com 我得到和你一样的输出。你在期待什么?
-
当我们在浏览器中打开amwasia.com时,我们将被重定向到amwmotors.com,所以我想获取这个重定向站点的html,而我们得到的那个不正确,你可以检查一下通过使用检查元素或使用 ctrl+u
标签: javascript node.js redirect request npm