如何使用与puppeteer

时间:2018-05-27 05:22:29

标签: puppeteer node-http-proxy

我是puppeteer和node的新手,试图使用puppeteer的代理来收集请求&回复,希望也是websocket通信,但到目前为止无法得到任何工作..

我正在尝试以下代码:

const puppeteer = require('puppeteer');
const httpProxy = require('http-proxy');
const url       = require('url');

let runProxy = async ()=> {
     // raise a proxy and start collecting req.url/response.statusCode 
};

let  run = async () => {
    await runProxy();
    const browser = await puppeteer.launch({
        headless: false,
        args: ['--start-fullscreen',
            '--proxy-server=localhost:8096']
    });
    page = await browser.newPage();
    await page.setViewport({ width: 1920, height: 1080 });
    await page.goto('http://www.google.com',
        {waitUntil: 'networkidle2', timeout: 120000});
};

run(); 

我尝试了https://github.com/nodejitsu/node-http-proxy的一些变化,但似乎对我没有任何帮助,需要一些指导,谢谢

1 个答案:

答案 0 :(得分:0)

尝试this,使用https-proxy-agent或http-proxy-agent代理每个页面的请求:

import {Job, Launcher, OnStart, PuppeteerUtil, PuppeteerWorkerFactory} from "../..";
import {Page} from "puppeteer";

class TestTask {

    @OnStart({
        urls: [
            "https://www.google.com",
            "https://www.baidu.com",
            "https://www.bilibili.com",
        ],
        workerFactory: PuppeteerWorkerFactory
    })
    async onStart(page: Page, job: Job) {
        await PuppeteerUtil.defaultViewPort(page);
        await PuppeteerUtil.useProxy(page, "http://127.0.0.1:2007");
        await page.goto(job.url);
        console.log(await page.evaluate(() => document.title));
    }

}

@Launcher({
    workplace: __dirname + "/workplace",
    tasks: [
        TestTask
    ],
    workerFactorys: [
        new PuppeteerWorkerFactory({
            headless: false,
            devtools: true
        })
    ]
})
class App {}