【问题标题】:Download files with goto in playwright-python在 playwright-python 中使用 goto 下载文件
【发布时间】:2022-10-21 02:18:11
【问题描述】:

有没有办法通过 playwright-python 中的 goto 直接转到其下载链接来下载文件?

async with page.expect_download(timeout=420000) as download_info:
        await page.goto(f'https://example.com/gateway/reports/{id}/file')

Download = await download_info.value
await Download.save_as(Download.suggested_filename)

【问题讨论】:

    标签: python automation download playwright playwright-python


    【解决方案1】:

    我发现的唯一方法是将 goto 包装在 try-except 块中

    from playwright.sync_api import Playwright, sync_playwright, expect
    
    with sync_playwright() as p:
        #We define the browser, the context and the page
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        page = browser.new_page()
        with page.expect_download() as download_info:
            #If we navigate without try, it will throw an exception and it will stop our script, so, we wrap it inside a try except block
            try:
                page.goto("https://manueltgomes.com/wp-content/uploads/2021/09/ParseHTMLLInks_1_0_0_1.zip")
            except:
                pass
        download = download_info.value
        download.save_as(f"./{download.suggested_filename}")
        context.close()
        browser.close()
    

    该文件将保存在 main.py 文件的相同路径中

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2020-07-09
      • 2021-09-25
      • 1970-01-01
      • 2022-07-14
      • 2022-10-02
      • 2022-01-23
      • 2022-06-11
      • 2022-07-12
      相关资源
      最近更新 更多