1

I understand how to call chrome with webbrowser to access a url, however, if that url led to a download, how would I automatically save the file to an assigned destination?

Here's what I have:

import webbrowser
import os

url = 'https://videos.com/test.mp4'
path = os.getcwd() + '/video.mp4'

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

file = webbrowser.get(chrome_path).open(url)
with open(path, "wb") as f:
    f.write(file)

This wouldn't work as chrome asks for a destination when accessing the url, I would like for chrome to automatically save the file based on the inputs of python. I understand I can do this manually using chrome, however, I'll be downloading many videos, which is why I would like to save time.

Is there any solution here or is this not possible?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Awais Shah
  • 17
  • 1
  • 6
  • To do this with `webbrowser` you should disable in Chrome the setting "prompt for download location", so then you would be able to save automatically the file without asking for the destination every time. However, if what you want is to programatically specify what is the destination, then I am unaware whether you can do that with `webbrowser`, but you can definitely do it with selenium: https://stackoverflow.com/questions/35331854/downloading-a-file-at-a-specified-location-through-python-and-selenium-using-chr – telex-wap Jan 26 '20 at 20:54
  • @telex-wap thanks for the response, I was thinking of using selenium, however, I was looking for python to access the current session of chrome rather than opening a new instance – Awais Shah Jan 26 '20 at 20:56

1 Answers1

0

I think You will find this to be a better way to interact with the browser.

from pyppeteer import launch
import asyncio

url = 'https://videos.com/test.mp4'

async def main():
    global browser

    #pyppeteer built-in chromium
    browser = await launch(headless=False)
    # or
    # browser = await launch() #for headless

    # or with Your native browser    
    # browser = await launch(headless=False, executablePath='C:\\Program Files 
    #(x86)\\Google\\Chrome\\Application\\chrome.exe', 
    #userDataDir="\\Local\\Google\\Chrome\\User Data")
    page = await browser.newPage()
    await page.goto(url)
    (code to target video selector and write video here)

    await browser.close()

run = asyncio.run(main())

Have you given yl-downloader a try with the target URls? That may be a simpler option all together

https://github.com/ytdl-org/youtube-dl/blob/master/README.md#readme

John Edens
  • 21
  • 1
  • 7
  • per your above comment:To access the current session of chrome see my answer at 'https://stackoverflow.com/questions/57957890/connect-with-pyppeteer-to-existing-chrome – John Edens Jan 26 '20 at 21:16