【发布时间】:2015-11-23 09:14:35
【问题描述】:
我想知道是否有人知道如何使用 Request npm 处理来自 bitly 或 tribal 或 Twitter 的 t.co URL 等网站的重定向。例如,如果我想使用 Request npm 抓取网页,而我必须到达该页面的链接是一个会重定向我的小 URL 或缩短的 URL,我该如何处理这些重定向?
我发现 Request npm 的“followRedirect”选项默认设置为 true。如果我将其设置为 false,我可以通过抓取返回的页面来获取该页面将我重定向到的下一个链接,但这不是最好的,因为我不知道我将要进行多少次重定向通过。
现在我收到 500 错误。当我将“followRedirect”设置为 true 时。当我将“followRedirect”设置为 false 时,我可以获得每个重定向页面。再说一次,我不知道我必须通过多少重定向页面。代码如下:
var options = {
followRedirect: false
};
request('http://t.co/gJ74UfmH4i', options, function(err, response, body){
// when options are set I get the redirect page
// when options are not set I get a 500
});
【问题讨论】:
-
我假设您的意思是 NodeJS 包名为 Request by Mikael (npmjs.com/package/request)?
-
是的,没错。
标签: javascript node.js redirect npm