如何将本指南实施到无头浏览器https://www.npmjs.com/package/user-agents#contributing

时间:2019-08-21 07:33:30

标签: javascript node.js puppeteer user-agent

这是用于生成随机用户代理的软件包:https://www.npmjs.com/package/user-agents

如何使用Puppeteer在无头chrome浏览器中实现此功能?

这是我随机生成的输出,但这仅记录到控制台,并且没有在无头浏览器中实现:

{
  "appName": "Netscape",
  "connection": {
    "downlink": 10,
    "effectiveType": "4g",
    "rtt": 0
  },
  "platform": "Win32",
  "pluginsLength": 3,
  "vendor": "Google Inc.",
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 
Safari/537.36",
  "viewportHeight": 660,
  "viewportWidth": 1260,
  "deviceCategory": "desktop",
  "screenHeight": 800,
  "screenWidth": 1280
}

我的Node.js代码:

const puppeteer = require('puppeteer');

// library from https://www.npmjs.com/package/user-agents#contributing
const UserAgent = require('user-agents');

// This is where we'll put the code to get around the tests.
const preparePageForTests = async (page) => {
// TODO: Not implemented yet.
  const userAgent = new UserAgent();
  console.log(userAgent.toString());
  console.log(JSON.stringify(userAgent.data, null, 2));
  await page.setUserAgent(userAgent.toString());

}

(async () => {
  // Launch the browser in headless mode and set up a page.
  const browser = await puppeteer.launch({
    args: ['--no-sandbox'],
    headless: true,
  });
  const page = await browser.newPage();

  // Prepare for the tests (not yet implemented).
  await preparePageForTests(page);

  // Navigate to the page that will perform the tests.
   const testUrl = 'https://intoli.com/blog/' +
  'not-possible-to-block-chrome-headless/chrome-headless-test.html';
  await page.goto(testUrl);

  // Save a screenshot of the results.
  await page.screenshot({path: 'C:\\Users\\Badar\\Desktop\\headless-test- 
 result.png'});

  // Clean up.
  await browser.close()
})();

1 个答案:

答案 0 :(得分:0)

我已经轻松编辑了您的代码,该代码现在对我来说是正确的:

const puppeteer = require('puppeteer');
const UserAgent = require('user-agents');

const preparePageForTests = async (page) => {
  const user = new UserAgent();
  await page.setUserAgent(String(user.data.userAgent));
  const currentAgent = await page.evaluate('navigator.userAgent');
  console.log(currentAgent);
}

(async () => {
  const browser = await puppeteer.launch({
    args: ['--no-sandbox'],
    headless: false,
  });
  const page = await browser.newPage();
  await preparePageForTests(page);
  const testUrl = 'https://intoli.com/blog/' +
    'not-possible-to-block-chrome-headless/chrome-headless-test.html';
  await page.goto(testUrl);
  await page.screenshot({ path: 'result.png' });
  await browser.close();
})();

根据您提供的控制台输出,我决定从user.data.userAgent抓取字符串。

我还添加了一些代码来检查是否成功设置了新的用户代理:

const currentAgent = await page.evaluate('navigator.userAgent');
console.log(currentAgent);

每次我运行文件时,此输出都会随机更改,如预期的那样。