一个笨蛋前端

好好学习,天天向上

基于request async写一个简单的nodejs登陆小爬虫

初始化

const fs = require('fs');
const writable = fs.createWriteStream('./file.txt');
const $ = require('request-promise').defaults({
    transform: (body, response) => {
        return response
    }
});
const request = require('request');
var url_login = 'http://s.u-gong.com/userlogin?username=13509853638&password=222';
// var url = 'http://s.u-gong.com/seller/myOrders?v=1496819220754';
var url = 'http://s.u-gong.com/cookie?_=1496826970878';

main函数

!async function main() {
    try {
        //登陆得到cookie

        let cookies = await login(url_login);
        let reqCookie = Reqcookie(cookies);
        //发起请求
        let data = await $.get({uri: url, headers: {Cookie: reqCookie}})
        writable.write(data.body, "UTF-8");
        writable.end('This is the end\n');
        writable.on('finish', namne => {
            console.error('All writes are now complete.');
        });
    } catch (e) {
        console.warn(e)
    }
}()

登陆获取cookie

async function login(url_login) {
    let res = await $.post(url_login);
    let cookies = res.headers["set-cookie"];
    let JSONck = {};
    cookies.forEach(ck => {
        let item = ck.split(';')[0];
        item = item.split("=")
        let key = item[0];
        JSONck[key] = item[1]
    });
    return JSONck
}

组装请求cookie

function Reqcookie(cookies) {
    let reqCookie = "";
    for (let key in cookies) {
        reqCookie = reqCookie + `${key}=${cookies[key]};`;
    }
    return reqCookie
}


点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注