← Docs / Proxies
Datacenter proxies with Playwright
Playwright takes the proxy as a launch option. This page uses Python and the datacenter tier: a US datacenter IP that changes for every new connection.
What you need
- Your proxy API key, from the Proxies page in the portal.
pip install playwright, thenplaywright install chromium.
export PB_PROXY_KEY="YOUR_KEY"
Launch a browser through the proxy
import os
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(
proxy={
"server": "http://proxy.privatebyte.com:3128",
"username": os.environ["PB_PROXY_KEY"],
"password": "datacenter",
}
)
page = browser.new_page()
page.goto("https://api.ipify.org")
print(page.inner_text("body")) # a US datacenter address
browser.close()
Playwright sends the credentials for you, so there is no extra authentication step.
Get a new IP
A browser keeps its connections open. Every page in the same context leaves from the same address. Open a new context to get a new one:
for _ in range(3):
context = browser.new_context()
page = context.new_page()
page.goto("https://api.ipify.org")
print(page.inner_text("body"))
context.close()
One page, several navigations: same IP. A new context for each visit: a new IP each time. Use the first when a site should see one steady visitor, and the second when each visit should look separate.
Know the limits
- The datacenter tier is US only and has no sessions. A site that checks the reputation of an address may block it. If you see
403or a challenge page, use a residential tier such asstandard. - A new IP does not clear cookies or storage. A new context does, which is part of why it works well here.
Errors
| What you see | What it means |
|---|---|
ERR_TUNNEL_CONNECTION_FAILED with a 407 |
The API key is wrong or missing. |
ERR_TUNNEL_CONNECTION_FAILED with a 400 |
The password asked for something this tier does not do: a country other than US, a session or a TTL. |
A page returns 403 |
The site is refusing datacenter addresses. |
A 400 that names an unknown tier |
The tier word is a close misspelling of a real tier, such as datacentre. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier. |
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.