【问题标题】:request nodejs module not supporting redirect for some url请求nodejs模块不支持某些url的重定向
【发布时间】: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


【解决方案1】:

如果您可以将body var 登录:

if (!error && response.statusCode == 200) {
   console.log("body>>>>>>"+body)
}

,这是因为您收到了200 状态码,这不是重定向。如果你通过浏览器测试了 url www.amwasia.com 并被重定向,是通过 html &lt;meta&gt; 标签完成的:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.amwmotors.com/">

通过请求模块,您似乎得到了200,而不是3xx

【讨论】:

  • 那么在这种情况下,如何使用请求模块获取重定向 url 的 html。
  • 好吧,由于您拥有的网址不会自动重定向您,而是通过html 进行重定向,我看到的唯一方法是废弃html,搜索@987654331 @ 标记并从 content 属性中获取 url。
猜你喜欢
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 2013-08-26
  • 1970-01-01
相关资源
最近更新 更多