node爬虫12306怎么处理self signed certificate in certificate chain错误
发布于 8 年前 作者 without-yy 6642 次浏览 来自 问答

QQ20170110-0@2x.pngasdassadasdsada.png

3 回复
opts = {
	...
	rejectUnauthorized: false
	...
}

@seasidesun 谢谢 已经ok

const rq = require('request-promise');
const async = require('async');


let arr = [16,17,18,19,20,22,23,24,25];
let train_code = 'D3125';
getData();


function getData() {//使用async控制并发
    async.mapLimit(arr, 3, function (url, callback) {
        request12306(url, callback);
    }, function (err, result) {
        getData();//全部结束后继续请求
    });
}

function request12306(day,cb) { //使用request-promise请求接口  回调函数cb是为了通知async已经结束当前请求
    let url = `https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=2017-01-${day}&leftTicketDTO.from_station=SZH&leftTicketDTO.to_station=SHH&purpose_codes=ADULT`;
    let opts = {
        url: url,
        method: 'GET',
        rejectUnauthorized: false,
        headers:{
            'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
        },
        timeout:1000
    };

    rq(opts).then((data) => {
        checkData(JSON.parse(data),day);
        cb();
    }).catch((err) => {
        cb();
        return '';
    });
}
function checkData(data,day) {
    // console.log(data);
    let info = null;
    if(data.status==true&&data.httpstatus==200){
        info = data.data;
    }
    for (let i = 0, len = info.length; i < len; i++) {
        let obj = info[i].queryLeftNewDTO;
        if(obj.station_train_code==train_code){
            if(obj.zy_num!='无'||obj.ze_num!='无'||obj.wz_num!='无'){
                console.log(`${day}日 ${train_code}有票`);
            }
        }
    }
}

回到顶部