javascript NodeJS - 如何从服务器响应中获取 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31514395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS - How to get cookies from server response
提问by MrD
I want to use nodeJS as tool for website scrapping. I have already implemented a script which logs me in on the system and parse some data from the page.
我想使用 nodeJS 作为网站抓取工具。我已经实现了一个脚本,它让我登录系统并解析页面中的一些数据。
The steps are defined like:
步骤定义如下:
Open login page
Enter login data
Submit login form
Go to desired page
Grab and parse values from the page
Save data to file
Exit
打开登录页面
输入登录数据
提交登录表单
转到所需页面
从页面中抓取并解析值
将数据保存到文件
出口
Obviously, the problem is that every time my script has to login, and I want to eliminate that. I want to implement some kind of cookie management system, where I can save cookies to .txt file, and then during next request I can load cookies from file and send it in request headers.
显然,问题是每次我的脚本都必须登录时,我想消除这种情况。我想实现某种 cookie 管理系统,在那里我可以将 cookie 保存到 .txt 文件,然后在下一个请求期间我可以从文件加载 cookie 并将其发送到请求标头中。
This kind of cookie management system is not hard to implement, but the problem is how to access cookies in nodejs? The only way I found it is using request response object, where you can use something like this:
这种cookie管理系统不难实现,但问题是如何在nodejs中访问cookie?我发现它的唯一方法是使用请求响应对象,您可以在其中使用以下内容:
request.get({headers:requestHeaders,uri: user.getLoginUrl(),followRedirect: true,jar:jar,maxRedirects: 10,},function(err, res, body) {
if(err) {
console.log('GET request failed here is error');
console.log(res);
}
//Get cookies from response
var responseCookies = res.headers['set-cookie'];
var requestCookies='';
for(var i=0; i<responseCookies.length; i++){
var oneCookie = responseCookies[i];
oneCookie = oneCookie.split(';');
requestCookies= requestCookies + oneCookie[0]+';';
}
}
);
Now content of variable requestCookies
can be saved to the .txt file and can loaded next time when script is executed, and this way you can avoid process of logging in user every time when script is executed.
现在变量的内容requestCookies
可以保存到.txt文件中,下次执行脚本时可以加载,这样就可以避免每次执行脚本时都需要登录用户的过程。
Is this the right way, or there is a method which returns cookies?
这是正确的方法,还是有一种返回 cookie 的方法?
NOTE: If you want to setup your request
object to automatically resend received cookies on every subsequent request, use the following line during object creation:
注意:如果您想将request
对象设置为在每个后续请求中自动重新发送收到的 cookie,请在对象创建期间使用以下行:
var request = require("request");
request = request.defaults({jar: true});//Send cookies on every subsequent requests
回答by Saeger
In my case, i've used 'http'library like the following:
就我而言,我使用了“http”库,如下所示:
http.get(url, function(response) {
variable = response.headers['set-cookie'];
})