Code Monkey home page Code Monkey logo

Comments (5)

EmadMokhtar avatar EmadMokhtar commented on May 15, 2024 4

It is indeed a Chrome feature, it the default download folder.
Did you try to get the Response object as binary, save it to a file on disk?

response = await page.goto("http://www.irs.gov/pub/irs-pdf/f1040.pdf")
with open("f1040.pdf", "wb") as new_file
    new_file.write(response)

Note: I'm not sure if this is working, I'm introducing a solution.

from pyppeteer.

miyakogi avatar miyakogi commented on May 15, 2024 2

Doesn't the same method work in pyppeteer?
That method is using chromium feature, so i guess it should work also in pyppeteer.

from pyppeteer.

sandou78 avatar sandou78 commented on May 15, 2024 1

after some debug, I found this way will work

cdp = await page.target.createCDPSession();
await cdp.send('Page.setDownloadBehavior', { 'behavior': 'allow', 'downloadPath': '/temp/'});

the reason is python has different grammer

you can't use behavior in python dict, you need use 'behavior'

from pyppeteer.

justinr1234 avatar justinr1234 commented on May 15, 2024 1

Converted the javascript function to python: puppeteer/puppeteer#299 (comment)

It isn't 1-to-1, as the generated random directory name is different, but the same number of characters. I had trouble figuring out how to convert Javascript's Number.toString function to Python.

import os
import random

def base36encode(number, alphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
        base36 = ""
        sign = ""

        if number < 0:
            sign = "-"
            number = -number

        if 0 <= number < len(alphabet):
            return sign + alphabet[number]

        while number != 0:
            number, i = divmod(number, len(alphabet))
            base36 = alphabet[i] + base36

        return sign + base36

async def download_file(page, f):
        randNum = random.random()
        intPart = int(str(randNum)[2:])
        base36num = base36encode(intPart)
        downloadPath = f"{os.getcwd()}/download-{base36num}"
        try:
            os.mkdir(downloadPath)
        except OSError as err:
            print(f"Creation of directory {downloadPath} failed: {err}")
        else:
            print(f"Successfully created download directory: {downloadPath}")

        cdp = await page.target.createCDPSession()
        await cdp.send(
            "Page.setDownloadBehavior",
            {"behavior": "allow", "downloadPath": downloadPath},
        )

        await f()

        print("Downloading...")
        fileName = ""
        theList = os.listdir(downloadPath)
        if len(theList) > 0:
            fileName = theList[0]
        while fileName is "" or fileName.endswith(".crdownload"):
            time.sleep(0.100)
            theList = os.listdir(downloadPath)
            if len(theList) > 0:
                fileName = theList[0]

        filePath = os.path.join(downloadPath, fileName)
        print(f"Downloaded file: {filePath}")
        return filePath

from pyppeteer.

tpoulton avatar tpoulton commented on May 15, 2024

It's been awhile since this was asked, however I think whats covered in this feature will get you what you need by interfacing with the DevTools Protocol directly: puppeteer/puppeteer#1770

from pyppeteer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.