Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用webbroswer.open()下载文件_Python_Python 3.x_Python Webbrowser - Fatal编程技术网

Python使用webbroswer.open()下载文件

Python使用webbroswer.open()下载文件,python,python-3.x,python-webbrowser,Python,Python 3.x,Python Webbrowser,我知道如何使用webbrowser调用chrome来访问url,但是,如果该url导致下载,我如何将文件自动保存到指定的目标 以下是我所拥有的: 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

我知道如何使用webbrowser调用chrome来访问url,但是,如果该url导致下载,我如何将文件自动保存到指定的目标

以下是我所拥有的:

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)
当chrome在访问url时请求一个目的地时,这是行不通的,我希望chrome能够根据python的输入自动保存文件。我知道我可以用chrome手动完成,但是,我会下载很多视频,这就是为什么我想节省时间


这里有什么解决方案吗?或者这是不可能的?

我想你会发现这是一种更好的与浏览器交互的方式

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())
你有没有试过yl downloader的目标URL? 这可能是一个更简单的选择


我想你会发现这是一种更好的与浏览器交互的方式

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())
你有没有试过yl downloader的目标URL? 这可能是一个更简单的选择


要使用
webbrowser执行此操作,您应该在Chrome中禁用“提示下载位置”设置,这样您就可以自动保存文件,而无需每次询问目的地。但是,如果您想通过编程指定目的地,那么我不知道您是否可以使用
webbrowser
,但您肯定可以使用selenium实现:@telex wap感谢您的回复,但是我正在考虑使用selenium,我想让python访问chrome的当前会话,而不是打开一个新实例。要使用
webbrowser
执行此操作,您应该在chrome中禁用“提示下载位置”设置,这样您就可以自动保存文件,而无需每次询问目的地。但是,如果您想通过编程指定目的地,那么我不知道您是否可以使用
webbrowser
,但您肯定可以使用selenium实现:@telex wap感谢您的回复,但是我正在考虑使用selenium,我想让python访问当前的chrome会话,而不是打开一个新的instanceper你的上述评论:访问当前的chrome会话,请参阅我的答案“根据你的上述评论:访问当前的chrome会话,请参阅我的答案”