Playwright Proxy Setup in Python: Rotation, Auth, and SOCKS5
Playwright Proxy Setup in Python: Rotation, Auth, and SOCKS5
Introduction
If you're working with web scraping or automation tasks that require proxies, setting up Playwright to use proxies is crucial. This guide will walk you through how to configure Playwright in Python for using proxies, including proxy rotation, handling authenticated proxies, and using SOCKS5 proxies.
We'll cover the following sections:
- Setting Up Playwright with a Proxy
- Rotating Proxies Per Page
- Handling Authenticated Proxies
- Using SOCKS5 Proxies
This guide assumes you have some familiarity with Python and have already installed the necessary libraries, including playwright. If not, you can install it using pip:
pip install playwright
Then, you need to install the browser binaries Playwright supports:
playwright install
Setting Up Playwright with a Proxy
To start using a proxy with Playwright in Python, you'll create a new browser context and specify the proxy server.
Basic Proxy Setup
Here's how you can set up a basic proxy:
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch()
context = browser.new_context(
proxy={
"server": "http://your-proxy-server:port",
}
)
page = context.new_page()
page.goto("https://example.com")
print(page.title())
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
Proxy Rotation
If you have multiple proxies and want to rotate them per page, you can create a function that generates new contexts with different proxy settings.
def setup_proxy_context(proxies):
def _setup_proxy_context(playwright):
browser = playwright.chromium.launch()
for proxy in proxies:
context = browser.new_context(
proxy={
"server": proxy,
}
)
page = context.new_page()
page.goto("https://example.com")
print(f"Page loaded with proxy {proxy}")
context.close()
browser.close()
return _setup_proxy_context
proxies = [
"http://proxy1.example.com:8080",
"http://proxy2.example.com:8080",
]
with sync_playwright() as playwright:
setup_proxy_context(proxies)(playwright)
Handling Authenticated Proxies
For authenticated proxies, you need to include the username and password in the proxy server URL.
def run_authenticated_proxy(playwright):
browser = playwright.chromium.launch()
context = browser.new_context(
proxy={
"server": "http://username:password@your-proxy-server:port",
}
)
page = context.new_page()
page.goto("https://example.com")
print(page.title())
context.close()
browser.close()
with sync_playwright() as playwright:
run_authenticated_proxy(playwright)
Using SOCKS5 Proxies
Playwright also supports SOCKS5 proxies. You just need to specify the server with the socks5:// protocol.
def run_socks5_proxy(playwright):
browser = playwright.chromium.launch()
context = browser.new_context(
proxy={
"server": "socks5://your-proxy-server:port",
}
)
page = context.new_page()
page.goto("https://example.com")
print(page.title())
context.close()
browser.close()
with sync_playwright() as playwright:
run_socks5_proxy(playwright)
Conclusion
In this guide, we've covered how to set up Playwright in Python with proxies, including proxy rotation, handling authenticated proxies, and using SOCKS5 proxies. These techniques are essential for web scraping and automation tasks that require proxies.
While manually managing proxy lists can be cumbersome, ProxyForge offers a solution by auto-testing and rotating proxies locally on your machine — one-off $29 unlock, no subscription:
https://proxyforge.dev/#download
This tool ensures you always have working proxies without the hassle of maintaining them yourself.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →