[Python] Simple download on click with scrapy-playwright

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    Just trying to download a simple text file on click.

    import scrapy
    from scrapy_playwright.page import PageMethod


    class DownloadSpider(scrapy.Spider):
    name = "download"

    def start_requests(self):
    yield scrapy.Request(
    url="https://the-internet.herokuapp.com/download",
    meta=dict(
    playwright=True,
    playwright_include_page = True,
    ),
    )

    async def parse(self, response):
    page = response.meta["playwright_page"]

    async with page.expect_download() as download_info:
    await page.get_by_text("lm-login.txt").click()
    download = await download_info.value

    await download.save_as(download.suggested_filename)


    getting the following error's as well as the file being blank and the terminal hanging.

    "in save_as
    file = await self._loop.run_in_executor(None, lambda: open(path, "wb"))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"

    "RuntimeError: Task <Task pending name='Task-72' coro=<DownloadSpider.parse() running at ..... attached to a different loop"


    I've been reading the documents and a few videos, but I can't find much info online, and I'm really new at this. Any help would be appreciated. I know it's something simple.

    Continue reading...

Compartilhe esta Página