How to set up a proxy for Playwright
Article provides helpful information for anyone looking to use proxies with Playwright, and offers some good advice for choosing the right approach.
There are different ways to set a proxy for Playwright.
If you are using Playwright for web scraping, it might be better to use the Crawlee framework, which simplifies the spider development process and includes a set of tools for working with proxies.
If you need to set a proxy directly in Playwright, there are two levels at which you can do so: the browser level and the context level. Let's consider all the options in order, depending on your specific needs and use case.
Browser level
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(
proxy={
"server": "http://127.0.0.1:8080"
}
)
response = browser.new_context().request.get(
"https://example.com",
timeout=5000
)
print(response.body())
import { chromium } from 'playwright';
(async () => {
const browser = await chromium.launch({
proxy: {server: 'http://127.0.0.1:8080'}
});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com/');
console.log(await page.content());
await context.close();
await browser.close();
})()
Context level
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context(proxy={
"server": "http://127.0.0.1:8080"
})
response = context.request.get(
"https://example.com",
timeout=5000
)
print(response.body())
import { chromium } from 'playwright';
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({
proxy: {server: 'http://127.0.0.1:8080'}
});
const page = await context.newPage();
await page.goto('https://example.com/');
console.log(await page.content());
await context.close();
await browser.close();
})()