首页 > 解决方案 > 是否可以更改新任务的代理

问题描述

我只是想知道是否可以更改新任务的代理,我无法确定是否或如何完成。

我试图搜索是否可以完成,但我找不到任何东西。

const { Cluster } = require("puppeteer-cluster");
const getProxy= require("./models/get-proxy.js");
const timeout = ms => new Promise(resolve => setTimeout(resolve), ms);

(async () => {
  const proxy = await getProxy();
  const type = socks[0].Type[0] == "https" ? "https" : "http";
  const pro = type + "://" + proxy[0].Ip + ":" + proxy[0].Port;
  console.log('proxy:', pro);
  
  const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_BROWSER,
    maxConcurrency: 1,
    puppeteerOptions: {
      headless: true,
      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
       `--proxy-server=${pro}`
      ]
    }
  });

  await cluster.task(async({ page, data: url }) => {    
    await page.goto(url);
    console.log('going to url:', url);
    
    const data = await page.evaluate(() => {
      return {
        ip: document.body.innerText,
      }
    });
    
    console.log('data:', data);
  });
  
  try {
    for (;; await timeout(10000)) {
      await cluster.execute('https://api.myip.com');
      await cluster.execute('https://api.myip.com');
      await cluster.execute('https://api.myip.com');
      // More urls
      await cluster.idle();
      await cluster.close();
    }
  } catch (error) {
    console.log('error:', error.message);
  }
})();

我想要实现的是在每个循环完成后更改代理,但我似乎无法弄清楚如何或是否可以完成。

标签: node.jspuppeteerpuppeteer-cluster

解决方案


推荐阅读