-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples_playwright.py
More file actions
27 lines (21 loc) · 917 Bytes
/
examples_playwright.py
File metadata and controls
27 lines (21 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import asyncio
from proxy_relay import create_proxy_async
from playwright.async_api import ProxySettings, async_playwright
UPSTREAM_PROXY = os.getenv("UPSTREAM_PROXY", "socks5://user:pass@proxy.com:1080")
TEST_URL = "https://api.ipify.org/"
print(UPSTREAM_PROXY)
async def main() -> None:
"""Use Playwright with a local proxy created by proxy_relay."""
# Convert upstream proxy to a local HTTP proxy
local_url = await create_proxy_async(UPSTREAM_PROXY, local_type="http")
async with async_playwright() as p:
proxy: ProxySettings = {"server": local_url}
browser = await p.chromium.launch(proxy=proxy, headless=False)
page = await browser.new_page()
await page.goto(TEST_URL)
text = await page.text_content("body")
print(text.strip() if text else "<empty>")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())