Add example headless browsing script
This commit is contained in:
26
headless_browser_example.py
Normal file
26
headless_browser_example.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""Example Python script for headless browsing using Playwright on Linux.
|
||||
|
||||
This script launches a headless Chromium browser, navigates to a webpage,
|
||||
prints the page title, and closes the browser.
|
||||
|
||||
Requirements:
|
||||
- Python 3.8+
|
||||
- Playwright library: pip install playwright
|
||||
- Install browsers: playwright install
|
||||
|
||||
Optimized for low resource usage.
|
||||
"""
|
||||
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
def main():
|
||||
with sync_playwright() as p:
|
||||
# Launch headless browser with minimal options for efficiency
|
||||
browser = p.chromium.launch(headless=True, args=['--no-sandbox', '--disable-gpu'])
|
||||
page = browser.new_page()
|
||||
page.goto('https://example.com')
|
||||
print(f"Page title: {page.title()}")
|
||||
browser.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user