Comments (5)
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.
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.
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.
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.
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)
- how to clear the input area?
- Page.cookies() returns List, not Dict HOT 1
- TypeError: 'coroutine' object is not callable, why? HOT 1
- Support ARM OS HOT 1
- Navigation Timeout Exceeded: 30000 ms exceeded HOT 7
- Error browser closes unexpectedly HOT 1
- Can I get innerHTML from the Element Handle Class? HOT 1
- Response.buffer() return str rather than [bytes] HOT 1
- Pyppeteer on python 3.6.8 HOT 1
- UTF-8 characters are coming as garbage HOT 1
- KeyError when send HOT 1
- SyntaxError: invalid escape sequence HOT 2
- Is pyppeteer still maintained? HOT 4
- --enable-automation HOT 1
- can we use the cookies in python request from puppeteer cookies? HOT 1
- Page goto returns None HOT 2
- Execution context was destroyed, most likely because of a navigation. HOT 2
- Is there a forum or something for specific question related to pyppeteer? HOT 1
- Screenshot is not working in headless HOT 1
- >> REPOSITORY ABANDONED >> use pyppeteer2 instead HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyppeteer.