How to maintain a request session in NodeJS

前端 未结 3 1691
猫巷女王i
猫巷女王i 2020-11-30 21:51

I\'m trying to use NodeJS to scrape a website that requires a login by POST. Then once I\'m logged in I can access a separate webpage by GET<

3条回答
  •  盖世英雄少女心
    2020-11-30 21:57

    The request.jar(); didn't work for me. So I am using the headers response to make another request like this:

    request.post({
        url: 'https://exampleurl.com/login',
        form: {"login":"xxxx", "password":"xxxx"}
    }, function(error, response, body){
    
        request.get({
            url:"https://exampleurl.com/logged",
            header: response.headers
        },function(error, response, body){
            // The full html of the authenticated page
            console.log(body);
        });
    });
    

    Actualy this way is working fine. =D

提交回复
热议问题