Search code examples
pythonplaywrightplaywright-python

How to download multiple excel files in same class name from website using Playwright


In this website (https://www.mca.gov.in/content/mca/global/en/data-and-reports/company-llp-info/incorporated-closed-month.html#) there are multiple excel files one by one with same class name with different value-data. I was download a single excel file from website using playwright click() function. I don't know how to download second file automatically after download first file.

Here the code,

import re
import asyncio
import requests
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless = False, slow_mo=50)
        page = await browser.new_page()

        web = "https://www.mca.gov.in/content/mca/global/en/data-and-reports/company-llp-info/incorporated-closed-month.html"

        await page.goto(web)

        await page.click('[class="expand-desk"]')

        async with page.expect_download() as download_info:
            await page.click('[class="doc-link download-file"]')

        download = await download_info.value

        print("download_url = ",download)

        new = re.search("(?P<url>https?://[^\s ' ]+)", str(download)).group("url")
        print("New url = ",new)

        Filename = new.rsplit('=')[1]+".xlsx"

        r = requests.get(new, allow_redirects=True)

        open(Filename, 'wb').write(r.content)

        await page.screenshot(path="report.png")

        await page.pause()
        await browser.close()

asyncio.run(main())

Can you please suggest any idea about these?


Solution

  • Maybe this simple example will help you:

    for CURRENT_XPATH in ['FIRST_XPATH', 'SECOND_XPATH']:
    
        with page.expect_download() as download_info:
            page.click(CURRENT_XPATH)
    
        Download = await download_info.value
        await Download.save_as(Download.suggested_filename)