简体   繁体   English

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

[英]how to implement this guide to headless browser https://www.npmjs.com/package/user-agents#contributing

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

How can I implement this in headless chrome browser, using Puppeteer? 如何使用Puppeteer在无头Chrome浏览器中实现此功能?

Here's my randomly generated output, but this only logged to the console and wasn't implemented in the headless browser: 这是我随机生成的输出,但这仅记录到控制台,并且没有在无头浏览器中实现:

{
  "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
}

My Node.js code: 我的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()
})();

I have lightly edited your code, which is now working correctly for me: 我已经轻松地编辑了您的代码,现在对我来说它可以正常工作:

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();
})();

Based on the console output you provided, I decided to grab the string from user.data.userAgent . 根据您提供的控制台输出,我决定从user.data.userAgent获取字符串。

I also added some code to check that the new user agent was set successfully: 我还添加了一些代码来检查是否成功设置了新的用户代理:

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

This output changed randomly every time I ran the file, as expected. 每次运行文件时,此输出都会随机更改,这与预期的一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现像https://www.yahoo.com这样的div自动修复 - How to implement auto fixing a div like https://www.yahoo.com npmjs.com 如何计算代码质量 - How npmjs.com calculates the code quality 如何实现类似www.igoogle.com这样的内容? - How to implement something like www.igoogle.com? iframe 通过 document.getElementById('iframe346').src = 'https://www.bing.com/' 加载时如何防止浏览器停止阻止弹出窗口; - How to prevent browser to stop blocking pop up when iframe loads through document.getElementById('iframe346').src = 'https://www.bing.com/'; 如何在多个标签上搜索npmjs.com软件包? - How can I search npmjs.com packages on multiple tags? 如何使用Access-Control-Allow-Origin:https://www.example.com? - How to use Access-Control-Allow-Origin: https://www.example.com? 抓取 https://www.nytimes.com 时出错。我该如何解决? - Error in scraping https://www.nytimes.com.how do I solve it? 如何使用javascript可视化人群中的一定数量的人,类似于https://www.7billionworld.com/ - How to visualise a certain amount of people in a crowd using javascript, similar to https://www.7billionworld.com/ 如何更换<link href="https://www.google.com/favicon.ico">在 html 中使用 jquery 或 javascript? - How to replace <link href="https://www.google.com/favicon.ico"> in html with jquery or javascript? Linking.openURL(“https://www.google.com/”) 无限循环(标签),我怎样才能获得单身? - Linking.openURL(“https://www.google.com/”) infinite loop(tabs) ,how can i get single?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM