Dual-Engine Quickstart & Production Recipes
Tested, copy-paste ready recipes for Python and Node.js automation scenarios on mobile hardware.
Recipe 1: Standard Asynchronous Web Scraping
🐍 Python:
import asyncio
from termux_playwright import async_playwright_termux, launch
async def main():
async with async_playwright_termux() as p:
browser = await launch(p, headless=True)
page = await browser.new_page()
await page.goto("https://news.ycombinator.com", timeout=45000)
print(f"Hacker News Title: {await page.title()}")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
☕ Node.js:
const { launch } = require('termux-playwright');
async function main() {
const browser = await launch({ headless: true });
const page = await browser.newPage();
await page.goto('https://news.ycombinator.com', { timeout: 45000 });
console.log('Hacker News Title:', await page.title());
await browser.close();
}
main().catch(console.error);
Recipe 2: Anti-Bot & Cloudflare Turnstile Stealth Evasion
🐍 Python:
import asyncio
from termux_playwright import async_playwright_termux, launch, setup_stealth_context
async def main():
async with async_playwright_termux() as p:
browser = await launch(p, headless=True, stealth=True)
context = await setup_stealth_context(
browser,
locale="en-US",
timezone_id="America/New_York",
extra_headers={"Accept-Language": "en-US,en;q=0.9"}
)
page = await context.new_page()
await page.goto("https://bot.sannysoft.com", timeout=60000)
print(f"Test Result: {await page.title()}")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
☕ Node.js:
const { launch, setupStealthContext } = require('termux-playwright');
async function main() {
const browser = await launch({ headless: true, stealth: true });
const context = await setupStealthContext(browser, {
locale: 'en-US',
timezoneId: 'America/New_York'
});
const page = await context.newPage();
await page.goto('https://bot.sannysoft.com', { timeout: 60000 });
console.log('Test Result:', await page.title());
await browser.close();
}
main().catch(console.error);
Recipe 3: 24/7 Resilient Infinite Daemon with WakeLock & Resource Blocking
🐍 Python:
import asyncio
from termux_playwright import async_playwright_termux, launch, block_heavy_resources
async def run_worker():
while True:
try:
async with async_playwright_termux() as p:
browser = await launch(p, headless=True, low_memory_mode=True, wake_lock=True)
page = await browser.new_page()
await block_heavy_resources(page, images=True, media=True, fonts=True)
await page.goto("https://example.com", timeout=45000, wait_until="domcontentloaded")
print(f"Processed: {await page.title()}")
await browser.close()
except Exception as e:
print(f"Recovering from cycle error: {e}")
await asyncio.sleep(60)
if __name__ == "__main__":
asyncio.run(run_worker())
☕ Node.js:
const { launch, blockHeavyResources, forceGarbageCollection } = require('termux-playwright');
async function runWorker() {
while (true) {
try {
const browser = await launch({ headless: true, lowMemoryMode: true, wakeLock: true });
const page = await browser.newPage();
await blockHeavyResources(page, { images: true, media: true, fonts: true });
await page.goto('https://example.com', { timeout: 45000, waitUntil: 'domcontentloaded' });
console.log('Processed:', await page.title());
await browser.close();
// Periodic memory purge
forceGarbageCollection();
} catch (e) {
console.error('Cycle recovery:', e);
}
await new Promise(r => setTimeout(r, 60000));
}
}
runWorker();