mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-29 08:44:59 +00:00
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:
parent
a2649cabe9
commit
d1782d1c87
1 changed files with 9 additions and 10 deletions
19
playw.py
19
playw.py
|
@ -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])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue