|
| 1 | +--- |
| 2 | +title: Generating fingerprints |
| 3 | +description: description |
| 4 | +menuWeight: 3 |
| 5 | +paths: |
| 6 | + - anti-scraping/mitigation/generating-fingerprints |
| 7 | +--- |
| 8 | + |
| 9 | +# [](#generating-fingerprints) Generating fingerprints |
| 10 | + |
| 11 | +With the [Fingerprint generator](https://github.com/apify/fingerprint-generator) NPM package, you can easily generate a browser fingerprint. |
| 12 | + |
| 13 | +> It is crucial to generate fingerprints for the specific browser and operating system being used to trick the protections successfully. For example, if you are trying to overcome protection locally with Firefox on a macOS system, you should generate fingerprints for Firefox and macOS to achieve the best results. |
| 14 | +
|
| 15 | +```JavaScript |
| 16 | +import FingerprintGenerator from 'fingerprint-generator'; |
| 17 | + |
| 18 | +// Instantiate the fingerprint generator with |
| 19 | +// configuration options |
| 20 | +const fingerprintGenerator = new FingerprintGenerator({ |
| 21 | + browsers: [ |
| 22 | + { name: "firefox", minVersion: 80 }, |
| 23 | + ], |
| 24 | + devices: [ |
| 25 | + "desktop" |
| 26 | + ], |
| 27 | + operatingSystems: [ |
| 28 | + "windows" |
| 29 | + ] |
| 30 | +}); |
| 31 | + |
| 32 | +// Grab a fingerprint from the fingerprint generator |
| 33 | +const { fingerprint } = fingerprintGenerator.getFingerprint({ |
| 34 | + locales: ["en-US", "en"] |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +## [](#injecting-fingerprints) Injecting fingerprints |
| 39 | + |
| 40 | +Once you've generated a fingerprint, it can be injected into the browser using the [Fingerprint injector](https://github.com/apify/fingerprint-injector) package. This tool allows you to inject fingerprints to browsers automated by Playwright or Puppeteer: |
| 41 | + |
| 42 | +```JavaScript |
| 43 | +import FingerprintGenerator from 'fingerprint-generator'; |
| 44 | +import { FingerprintInjector } from 'fingerprint-injector'; |
| 45 | +import { chromium } from 'playwright'; |
| 46 | + |
| 47 | +// Instantiate a fingerprint injector |
| 48 | +const fingerprintInjector = new FingerprintInjector(); |
| 49 | + |
| 50 | +// Launch a browser in Playwright |
| 51 | +const browser = await chromium.launch(); |
| 52 | + |
| 53 | +// Instantiate the fingerprint generator with |
| 54 | +// configuration options |
| 55 | +const fingerprintGenerator = new FingerprintGenerator({ |
| 56 | + browsers: [ |
| 57 | + { name: "firefox", minVersion: 80 }, |
| 58 | + ], |
| 59 | + devices: [ |
| 60 | + "desktop" |
| 61 | + ], |
| 62 | + operatingSystems: [ |
| 63 | + "windows" |
| 64 | + ] |
| 65 | +}); |
| 66 | + |
| 67 | +// Grab a fingerprint |
| 68 | +const { fingerprint } = fingerprintGenerator.getFingerprint({ |
| 69 | + locales: ["en-US", "en"] |
| 70 | +}); |
| 71 | + |
| 72 | +// Create a new browser context, plugging in |
| 73 | +// some values from the fingerprint |
| 74 | +const context = await browser.newContext({ |
| 75 | + userAgent: fingerprint.userAgent, |
| 76 | + locale: fingerprint.navigator.language, |
| 77 | +}); |
| 78 | + |
| 79 | +// Attach the fingerprint to the newly created |
| 80 | +// browser context |
| 81 | +await fingerprintInjector.attachFingerprintToPlaywright(context, fingerprint); |
| 82 | + |
| 83 | +// Create a new page and go to Google |
| 84 | +const page = await context.newPage(); |
| 85 | +await page.goto('https://google.com'); |
| 86 | +``` |
| 87 | + |
| 88 | +> Note that the Apify SDK automatically applies wide variety fingerprints by default, so it is not required to do this unless you aren't using the Apify SDK or if you need a super specific custom fingerprint to scrape with. |
| 89 | +
|
| 90 | +## [](#next) Next up |
| 91 | + |
| 92 | +That's it for the **Mitigation** course for now, but be on the lookout for future lessons! We release lessons as we write them, and will be updating the Academy frequently, so be sure to check back every once in a while for new content! Alternatively, you can subscribe to our mailing list to get periodic updates on the Academy, as well as what Apify is up to. |
0 commit comments