W4J音乐热评一Puppeteer设置代理IP

2020-06-06  本文已影响0人  爱吃馒头不吃辣

阿布云HTTP隧道

购买隧道服务

隧道服务使用方法

const puppeteer = require('puppeteer');
const mapLimit = require('async/mapLimit');
const initPuppeteerPool = require('./genericPool');
let musicId = [1407551413, 1303289043, 1417862046];
//设置通行证书和同行密钥,格式一定是要username和password
let agencyIp = {
    username: 'HG*32T**34*8Q*VD',
    password: 'CC4*79*1*15*AE*C'
}
const pool = initPuppeteerPool({
    puppeteerArgs: {
        timeout: 15000,
        ignoreHTTPSErrors: true,
        devtools: true,
        headless: false,
        args: [
            '-–disable-dev-shm-usage',
            '-–disable-setuid-sandbox',
            '-–no-first-run',
            '--no-sandbox',
            '-–no-zygote',
            '-–single-process',
            //设置HTTP隧道服务地址
            '--proxy-server=http://http-dyn.abuyun.com:9020'
        ]
    }
})
mapLimit(musicId, 3, (item, callback) => {
    (async () => {
        let info = await selectInfo(item);
        callback(null, info);
    })();
}, (err, results) => {
    console.log(results);
});
async function selectInfo(id) {
    return new Promise(async (resolve, reject) => {
        const page = await pool.use(async browser => {
            const page = await browser.newPage();
            //使用通行证书和通行密钥登录服务器
            await page.authenticate(agencyIp);
            await page.goto(`https://music.163.com/#/song?id=${id}`);
            return page
        })
        const iframe = await page.frames().find(f => f.name() === 'contentFrame');
        const musicComment = await iframe.$('.cmmts.j-flag');
        let commentList = await iframe.evaluate((e) => {
            let comment = Array.from(e.getElementsByClassName('cnt f-brk'));
            return comment.map((item) => item.innerText).filter((item, index) => index <= 5);
        }, musicComment);
        await page.close();
        resolve(commentList);
    })
}
上一篇 下一篇

猜你喜欢

热点阅读