我在 python 中遇到 wget 模块问题

I have problem with wget module in python

有人能解决我的代码吗?我太菜了 另外,如果您告诉我如何更有效地完成它而不必为每种文件类型创建不同的函数,我将非常高兴。

(你可以把这段代码想象成 1990 年代互联网不好时存在的研究设施的终端,所以我想放慢下载速度,让它感觉真的像 90 年代。 )

import wget

x = ""

def png_download():
    print("this code is can only download png btw")
    time.sleep(1)
    
    print("enter url")
    command = input('Terminal: ').strip()
    command = x
    
    
    # file url
    remote_url = ("'"+x+"'")
    
    # 
    local_file = 'file.png'
    
    # aksiyon
    wget.download(remote_url, local_file)

while True: #Terminal
    print ("")
    command = input('Terminal: ').strip()
    if command == 'Download_png':
        png_download()
        
    else:
        print("")   

如果我 运行 代码并输入 url 会发生这种情况:

  File "c:\Dosyalar\Yazılım\Python\Foundation Terminal 14\Terminal Downloader Extension ENG .py", line 28, in <module>
    png_download()
  File "c:\Dosyalar\Yazılım\Python\Foundation Terminal 14\Terminal Downloader Extension ENG .py", line 22, in png_download
    wget.download(remote_url, local_file)
  File "C:\Users\hp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\wget.py", line 526, in download
    (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 239, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 501, in open
    req = Request(fullurl, data)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 320, in __init__
    self.full_url = url
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 346, in full_url
    self._parse()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 375, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '%27%27' ```

使用 Python 下载文件更简单:

import os
import requests

def download_file_to(file_url, destination_folder, new_file_name=None):
    if new_file_name:
        file_name = new_file_name
    else:
        file_name = file_url.split("/")[-1]
    r = requests.get(file_url)
    file_path = os.path.join(destination_folder, file_name)
    with open(file_path, "wb") as code:
        code.write(r.content)

此方法将从网络(使用 requests 库)下载任何文件到您指定的文件夹中,您可以根据需要重命名该文件。

ValueError: unknown url type: '%27%27' ```

27' 字符的(十六进制)代码,这意味着您为 wget.download 提供的第一个参数只是 '',这显然不是合法地址。这是因为您将 x 设置为空字符串 (x = "") 并且无处更改,然后

remote_url = ("'"+x+"'")

wget.download(remote_url, local_file)

假设用户总是给出合法的URL,那么你的png_download应该修改为

def png_download():
    print("this code is can only download png btw")
    time.sleep(1)
    
    print("enter url")
    remote_url = input('Terminal: ').strip()
    # 
    local_file = 'file.png'
    
    # aksiyon
    wget.download(remote_url, local_file)