|
1 |
| -# playwright-proxy-integration-js |
2 |
| -A tutorial for implementing Oxylabs |
| 1 | +# Oxylabs' Proxy Integration with Playwright |
| 2 | + |
| 3 | +[<img src="https://img.shields.io/static/v1?label=&message=JavaScript&color=brightgreen" />](https://github.com/topics/python) [<img src="https://img.shields.io/static/v1?label=&message=Web%20Scraping&color=important" />](https://github.com/topics/web-scraping) [<img src="https://img.shields.io/static/v1?label=&message=Rotating%20Proxies&color=blueviolet" />](https://github.com/topics/rotating-proxies) |
| 4 | +- [Requirements](#requirements) |
| 5 | +- [Integrating Datacenter Proxies](#integrating-datacenter-proxies) |
| 6 | +- [Integrating Residential Proxies](#integrating-residential-proxies) |
| 7 | +## Requirements |
| 8 | + |
| 9 | +### Playwright |
| 10 | +```bash |
| 11 | +npm install playwright |
| 12 | +``` |
| 13 | + |
| 14 | +## Integrating Datacenter Proxies |
| 15 | + |
| 16 | +### Getting a List of Proxies |
| 17 | + |
| 18 | +Open the following URL in the browser, enter your credentials, and you will see a list of proxies in plain text: |
| 19 | + |
| 20 | +``` |
| 21 | +https://proxy.oxylabs.io/all |
| 22 | +``` |
| 23 | + |
| 24 | +### Using Proxies |
| 25 | + |
| 26 | +If you wish to select any one of the proxies, save the proxy IP, along with the port in a variable. |
| 27 | + |
| 28 | +To use all these proxies, first, save these proxies as an array in your code: |
| 29 | + |
| 30 | +```javascript |
| 31 | +let proxies = [ |
| 32 | + '127.0.0.1:60000', |
| 33 | + '127.0.0.2:60000', |
| 34 | + '127.0.0.3:60000', |
| 35 | + '127.0.0.4:60000' |
| 36 | + ] |
| 37 | +``` |
| 38 | + |
| 39 | +To select one of these randomly, use the following line of code: |
| 40 | + |
| 41 | +```JavaScript |
| 42 | +var proxy = proxies[Math.floor(Math.random() * proxies.length)]; |
| 43 | +``` |
| 44 | + |
| 45 | +create a variable called `launchOptions` and assign the proxy information to it. |
| 46 | + |
| 47 | +Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials. |
| 48 | + |
| 49 | +```javascript |
| 50 | +const launchOptions = { |
| 51 | + proxy: { |
| 52 | + server: proxy, |
| 53 | + username: 'USERNAME', |
| 54 | + password: 'PASSWORD' |
| 55 | + } |
| 56 | + }; |
| 57 | +``` |
| 58 | + |
| 59 | +After creating the `launchOptions` variable, create a `playwright` instance and launch the browser. |
| 60 | + |
| 61 | +```javascript |
| 62 | +const browser = await chromium.launch(launchOptions); |
| 63 | +``` |
| 64 | + |
| 65 | +For the complete code sample, see [this file](datacenter_random.js). |
| 66 | + |
| 67 | + |
| 68 | +## Integrating Residential Proxies |
| 69 | + |
| 70 | +### Random Proxy Using the Proxy API |
| 71 | +Proxy information can be supplied as a parameter to the `launch` method of `playwright` instance. |
| 72 | + |
| 73 | +Alternatively, create a variable called `launchOptions` and assign the proxy information to it. |
| 74 | + |
| 75 | +Don't forget to replace `USERNAME` and `PASSWORD` with your proxy user credentials. |
| 76 | + |
| 77 | +```javascript |
| 78 | +const launchOptions = { |
| 79 | + proxy: { |
| 80 | + server: 'pr.oxylabs.io:7777', |
| 81 | + username: 'USERNAME', |
| 82 | + password: 'PASSWORD' |
| 83 | + }, |
| 84 | + headless: false |
| 85 | + }; |
| 86 | +``` |
| 87 | + |
| 88 | +The additional benefit of using lauchOptions variable is that other information such as `headless` can be supplied to the `launch` method.: |
| 89 | + |
| 90 | +After creating the `launchOptions` variable, create a `playwright` instance and launch the browser. |
| 91 | + |
| 92 | +```javascript |
| 93 | +const browser = await chromium.launch(launchOptions); |
| 94 | +``` |
| 95 | + |
| 96 | +For the complete code sample, see [this file](residential_random.js). |
| 97 | + |
| 98 | +### Country Specific Proxies |
| 99 | + |
| 100 | +If you wish to use country-specific proxies, all you need to do is to change the `proxy` `server`. |
| 101 | + |
| 102 | +For example, if you wish to use the proxy for the United States, you can use the following code: |
| 103 | + |
| 104 | +```javascript |
| 105 | +const launchOptions = { |
| 106 | + proxy: { |
| 107 | + server: 'us-pr.oxylabs.io:10001', |
| 108 | + username: 'USERNAME', |
| 109 | + password: 'PASSWORD' |
| 110 | + }, |
| 111 | + headless: false |
| 112 | + }; |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +In this example, `us-pr.oxylabs.io:10000` is the country specific entry point for United States. |
| 117 | + |
| 118 | +Another example is `gb-pr.oxylabs.io:20000`, which is the country specific entry point for United Kingdom. |
| 119 | + |
| 120 | +For a complete list of all entry points, see [Country Specific Entry Nodes](https://developers.oxylabs.io/residential-proxies/#country-specific-entry-nodes). |
0 commit comments