【问题标题】:Pytube output pathPytube 输出路径
【发布时间】:2021-07-17 13:32:05
【问题描述】:

我的 pytube 输出路径有问题,当我将 output_path 设置为 C:/Users/%UserProfile%/Desktop 视频停止下载时,代码有什么问题?

import PySimpleGUI as sg
import re
from pytube import YouTube
sg.theme('Dark')

layout = [
            [sg.Text('Enter Youtube video link'), sg.InputText()],
            [sg.Button('Download'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Youtube Downloader', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel':  # if user closes window or clicks cancel
        break
    if re.search(r'\byoutube.com\b', values[0]) or re.search(r'\byoutu.be\b', values[0]):
        video = YouTube(values[0])
        YouTube(values[0]).streams.filter(res="1080p").first().download(output_path="C:/Users/%UserProfile%/Desktop")
        sg.popup('You downloaded: ', video.title)
    else:
        sg.popup('This is not a Youtube link, please try again  ')
window.close()

【问题讨论】:

    标签: python python-3.x pytube


    【解决方案1】:

    字符串被解释为文字。它不会改变。 %UserProfile% - 这将是 cmd 上的环境变量 - 不会被解释(正如您可能猜到的那样)。

    解决方案是使用os.environos.getenv(你必须import os 模块)将所有变量保留在当前环境中:

    output_path=f"{os.environ['UserProfile']}/Desktop"
    

    另外,os.environ['UserProfile'] 返回用户目录的完整路径,所以不需要前缀C:/Users/

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多