如何使用pyQt 5 webengine和python脚本代码下载文件?

时间:2022-11-15 23:03:59

so i want to make auto download when i got some link, let say the link is : http://test.com/somefile.avi

所以当我得到一些链接时我想自动下载,请说链接是:http://test.com/somefile.avi

import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QWidgetAction
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEngineDownloadItem, QWebEnginePage


class WebPage(QWebEngineView):
    def __init__(self):
        QWebEngineView.__init__(self)
        self.load(QUrl("http://test.com"))
        self.loadFinished.connect(self._on_load_finished)
        self.n = 0

    def _on_load_finished(self):
        print("Finished Loading")
        self.page().toHtml(self.Callable)

    def Callable(self, html_str):
        self.html = html_str
        self.load(QUrl(userInput))

if __name__ == "__main__":
    userInput = input()
    app = QApplication(sys.argv)
    web = WebPage()

except i only have the page 'test.com', but i cant get the file 'somefile.avi', is it possible to make it autodownload after i input the 'http://test.com/somefile.avi' in console?

除了我只有页面'test.com',但我不能得到文件'somefile.avi',我可以在控制台输入'http://test.com/somefile.avi'后自动下载?

Thanks

1 个解决方案

#1


0  

Below is a code snippet of how to do this with the requests library

下面是如何使用请求库执行此操作的代码段

DISCLAIMER

This example was made with requests, python 3rd party library, and not with PyQt as the asker originally intended.

这个例子是使用请求,python第三方库,而不是作为提问者最初打算使用的PyQt。

import requests
import shutil

def download(url):

    # gets the filename from the url, and
    # creates the download file absolute path
    filename = url.split("/")[-1]
    path = "downloads/" + filename

    # Defines relevant proxies, see `requests` docs
    proxies = {
      'http': 'http://10.10.1.10:3128',
      'https': 'http://10.10.1.10:1080',
    }

    # Add proxies, and leave `stream=True` for file downloads
    r = requests.get(url, stream=True, proxies=proxies)
    if r.status_code == 200:
        with open(path, 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    else:
        # Manually raise if status code is anything other than 200
        r.raise_for_status()


download('http://test.com/somefile.avi')

Edit:

pac files do not work out of the box with any of the common python web request libraries, however, SO user @CarsonLam provided an answer here that attempts to solve this issue.

pac文件不能与任何常见的python Web请求库一起使用,但是,SO用户@CarsonLam提供了一个试图解决此问题的答案。

The library pypacprovides support for this, and since it inherits from requests objects, it would macigally work with our existing code. Some additional pac examples can be found here.

库pypac提供了对此的支持,并且由于它继承自请求对象,因此它可以使用我们现有的代码。这里可以找到一些额外的pac示例。

With a pac proxy file, I would guess something like this would be the way to go;

使用pac代理文件,我猜想这样的事情是可行的;

from pypac import PACSession, get_pac
import shutil

def download(url):

    # gets the filename from the url, and
    # creates the download file absolute path
    filename = url.split("/")[-1]
    path = "downloads/" + filename

    # looks for a pac file at the specified url, and creates a session
    # this session inherits from requests.Session
    pac = get_pac(url='http://foo.corp.local/proxy.pac')
    session = PACSession(pac)

    # Add proxies, and leave `stream=True` for file downloads
    session = requests.get(url, stream=True)
    if r.status_code == 200:
        with open(path, 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    else:
        # Manually raise if status code is anython other than 200
        r.raise_for_status()

#1


0  

Below is a code snippet of how to do this with the requests library

下面是如何使用请求库执行此操作的代码段

DISCLAIMER

This example was made with requests, python 3rd party library, and not with PyQt as the asker originally intended.

这个例子是使用请求,python第三方库,而不是作为提问者最初打算使用的PyQt。

import requests
import shutil

def download(url):

    # gets the filename from the url, and
    # creates the download file absolute path
    filename = url.split("/")[-1]
    path = "downloads/" + filename

    # Defines relevant proxies, see `requests` docs
    proxies = {
      'http': 'http://10.10.1.10:3128',
      'https': 'http://10.10.1.10:1080',
    }

    # Add proxies, and leave `stream=True` for file downloads
    r = requests.get(url, stream=True, proxies=proxies)
    if r.status_code == 200:
        with open(path, 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    else:
        # Manually raise if status code is anything other than 200
        r.raise_for_status()


download('http://test.com/somefile.avi')

Edit:

pac files do not work out of the box with any of the common python web request libraries, however, SO user @CarsonLam provided an answer here that attempts to solve this issue.

pac文件不能与任何常见的python Web请求库一起使用,但是,SO用户@CarsonLam提供了一个试图解决此问题的答案。

The library pypacprovides support for this, and since it inherits from requests objects, it would macigally work with our existing code. Some additional pac examples can be found here.

库pypac提供了对此的支持,并且由于它继承自请求对象,因此它可以使用我们现有的代码。这里可以找到一些额外的pac示例。

With a pac proxy file, I would guess something like this would be the way to go;

使用pac代理文件,我猜想这样的事情是可行的;

from pypac import PACSession, get_pac
import shutil

def download(url):

    # gets the filename from the url, and
    # creates the download file absolute path
    filename = url.split("/")[-1]
    path = "downloads/" + filename

    # looks for a pac file at the specified url, and creates a session
    # this session inherits from requests.Session
    pac = get_pac(url='http://foo.corp.local/proxy.pac')
    session = PACSession(pac)

    # Add proxies, and leave `stream=True` for file downloads
    session = requests.get(url, stream=True)
    if r.status_code == 200:
        with open(path, 'wb') as f:
            r.raw.decode_content = True
            shutil.copyfileobj(r.raw, f)
    else:
        # Manually raise if status code is anython other than 200
        r.raise_for_status()