當前位置:網站首頁>node request模塊cookie使用

node request模塊cookie使用

2022-06-23 08:49:35An_s

前言

很多網站都有302或者301仿爬蟲, 響應頭就會帶上set-cookie, 那我們用node如何處理呢?

開始

簡潔版

let request = require("request");

// 開啟記錄cookie,重定向時自動帶上cookie
request = request.defaults({jar: true});


function login_redirect(url) {
    console.log("url: ", url);
    let options = {
        url: url,
        method: "GET",
        headers: {
            "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
        },
        // 重定向
        followRedirect: false
    }
    request(options, function(error, response, body) {
        console.log("...login_redirect.....");

        // console.log(response.statusCode);
        // console.log(response.headers);

        if (response.statusCode == 302 || response.statusCode == 301) {
            login_redirect(response.headers.location);
        } else {
            console.log("...跳轉完...");
            // console.log(response);
            // console.log(body);
        }
    });
}

文件版

let request = require("request");

// 在根目錄創建cookie.json文件
const FileCookieStore = require('tough-cookie-filestore');
const cookieJsonStore = new FileCookieStore('cookies.json')
const j = request.jar(cookieJsonStore);
request = request.defaults({ jar : j })


function login_redirect(url) {
    console.log("url: ", url);
    let options = {
        url: url,
        method: "GET",
        headers: {
            "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
        },
        // 重定向
        followRedirect: false
    }
    request(options, function(error, response, body) {
        console.log("...login_redirect.....");

        // console.log(response.statusCode);
        // console.log(response.headers);

        if (response.statusCode == 302 || response.statusCode == 301) {
            login_redirect(response.headers.location);
        } else {
            console.log("...跳轉完...");
            // console.log(response);
            // console.log(body);
        }
    });
}

推薦版

let request = require("request");

const j = request.jar();
request = request.defaults({jar:j});


function login_redirect(url) {
    console.log("url: ", url);
    let options = {
        url: url,
        method: "GET",
        headers: {
            "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'
        },
        followRedirect: false
    }
    request(options, function(error, response, body) {
        console.log("...login_redirect.....");

        // console.log(response.statusCode);
        // console.log(response.headers);

        if (response.statusCode == 302 || response.statusCode == 301) {
            login_redirect(response.headers.location);
        } else {
            console.log("...跳轉完...");
            // console.log(response);
            // console.log(body);
            
            console.log("cookie為:", j.getCookieString(url))
        }
    });
}

版權聲明
本文為[An_s]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/174/202206230837022156.html

隨機推薦