aider/playw.py
Paul Gauthier a2649cabe9 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 <URL>")
+        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 <URL>`, replacing `<URL>` with the actual URL you want to fetch text from.
2024-02-08 07:20:50 -08:00

18 lines
498 B
Python

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 <URL>")
sys.exit(1)
asyncio.run(main(sys.argv[1]))