From a2649cabe9c32a1c7d1d0e5fe68858edb8bf5b7e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 8 Feb 2024 07:20:50 -0800 Subject: [PATCH] aider: Added Playwright script to fetch text from a supplied URL. # Aider chat conversation: USER: write a playwright script to fetch the text from a url supplied on the cmd line ASSISTANT: To create a Playwright script that fetches text from a URL supplied on the command line, I will write the necessary code in the `playw.py` file. The script will use Playwright's Python package to launch a browser, navigate to the provided URL, and print the text content of the page to the console. Here is the diff for the new `playw.py` file: ```diff --- /dev/null +++ playw.py @@ ... @@ +import asyncio +from playwright.async_api import async_playwright +import sys + +async def main(url): + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.goto(url) + content = await page.content() + print(content) + await browser.close() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python playw.py ") + sys.exit(1) + asyncio.run(main(sys.argv[1])) ``` This script will require the Playwright package to be installed. You can install it using `pip install playwright` and then run `playwright install` to install the necessary browser binaries. To run the script, use the command `python playw.py `, replacing `` with the actual URL you want to fetch text from. --- playw.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 playw.py diff --git a/playw.py b/playw.py new file mode 100644 index 000000000..484f6b599 --- /dev/null +++ b/playw.py @@ -0,0 +1,18 @@ +import asyncio +from playwright.async_api import async_playwright +import sys + +async def main(url): + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.goto(url) + content = await page.content() + print(content) + await browser.close() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python playw.py ") + sys.exit(1) + asyncio.run(main(sys.argv[1]))