aider: Remove asynchronous code and use synchronous Playwright API.

# Aider chat conversation:

USER: don't use async

ASSISTANT: Here is the updated `playw.py` file without using async:

```diff
--- playw.py
+++ playw.py
@@ ... @@
-import asyncio
-from playwright.async_api import async_playwright
+from playwright.sync_api import sync_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()
+def main(url):
+    with sync_playwright() as p:
+        browser = p.chromium.launch()
+        page = browser.new_page()
+        page.goto(url)
+        content = page.content()
+        print(content)
+        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]))
+    main(sys.argv[1])
```

This diff removes the asynchronous code and replaces it with the synchronous version provided by Playwright's `sync_api`. The `async` and `await` keywords are removed, and the `async_playwright` context manager is replaced with `sync_playwright`. The script should now run synchronously.
This commit is contained in:
Paul Gauthier 2024-02-08 07:21:23 -08:00
parent a2649cabe9
commit d1782d1c87

View file

@ -1,18 +1,17 @@
import asyncio
from playwright.async_api import async_playwright
from playwright.sync_api import sync_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()
def main(url):
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(url)
content = page.content()
print(content)
await browser.close()
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]))
main(sys.argv[1])