pilip h
pilip h

Reputation: 13

How can I download file using pyQt 5 webengine with python script code?

so i want to make auto download when i got some link, let say the link is : 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?

Thanks

Upvotes: 1

Views: 3420

Answers (1)

Pax Vobiscum
Pax Vobiscum

Reputation: 2639

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.

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.

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.

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

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()

Upvotes: 1

Related Questions