Puppeteer browser useragent list

后端 未结 1 1066
执笔经年
执笔经年 2021-01-28 21:51

I would like to load random list of user-agents from my default location path , for example: \'agents.json\' instead of adding direct and only 1 user-agent.

agent

1条回答
  •  星月不相逢
    2021-01-28 22:09

    You seem to redeclare browser when you set the UA, which you shouldn't. Also you don't need to set it as an object: as page.setUserAgent accept a string: page.setUserAgent(userAgent).

    await page.setUserAgent(randomAgent); will be just fine.

    const browser = await puppeteer.getBrowserInstance(port);
    const randomReferer = referers[Math.floor(Math.random() * referers.length)];
    const randomAgent = agents[Math.floor(Math.random() * agents.length)]; // made it singular instead of plural
    const page = await browser.newPage();
    await page.setUserAgent(randomAgent); // like this
    page.setDefaultTimeout(PAGE_DEFAULT_TIMEOUT * 1000);
    page.on('error', handlePageCrash(page));
    page.on('pageerror', handlePageCrash(page));
    page.setExtraHTTPHeaders({ referer: randomReferer });
    

    Note: I think it is only a mistake in your post, but you also have two const named referers, this one should be the agents:

    const referers = require('../core/agents.json');
    

    0 讨论(0)
提交回复
热议问题