【问题标题】:How to get user input for a python script and use it in another program?如何获取 python 脚本的用户输入并在另一个程序中使用它?
【发布时间】:2021-11-04 11:13:12
【问题描述】:

我想获取在另一个程序中使用的用户输入作为 url 和路径的输入。但这似乎是不可能的。 #mymodule.py 脚本

from pytube import YouTube
import pytube
# import check_avail 

class vdodownload:

    def __init__(self,url,path):
        self.url = url
        self.path = path


    def link(self):
        try:
            print(f'Downloading video: {YouTube(self.url).title}')
            # yt = check_avail.video(self.url,self.path)
            YouTube(self.url).streams.first().download(self.path)
            print(f'Downloaded video: {YouTube(self.url).title}')
        
        except pytube.exceptions.ExtractError:
             print(f'Video {self.url} is unavaialable, skipping.')
        # else:
        #      print(f'Downloading video: {self.url}')
        #      yt.streams.first().download(self.path)
        #      print(f'Downloaded video: {self.url}')
        
            
url = input("Enter the video URL: ")
path = input("Enter the path: ")

t = vdodownload(url,path)

t.link()

#check_avail.py 脚本

from pytube import YouTube

if __name__ == "__main__":
    def video(url,path):
        y = YouTube(url) 
        # video1 = YouTube(url).streams.first().download(path)
        # print(f"title:{YouTube(url).title}, views:{YouTube(url).views}, Length:{YouTube(url).length}")
        print("Successfully done")    
        return video
else:
    def check(func):
        def parameters(u,v):
            print(f"title:{YouTube(u).title}, views:{YouTube(u).views}, Length:{YouTube(u).length}")
            return func(u,v)
        return parameters
        # return check

    def filtering(func):
        def param(u,v):
            check1 = YouTube(u).streams.filter(type='video',progressive=True,file_extension='mp4')
            print(check1)
            return func(u,v)
        return param
    # return filtering

    @filtering
    @check
    def video(url,path):
        y = YouTube(url) 
    # video1 = YouTube(url).streams.first().download(path)
        print("Success") 
        return video  


url = input("Enter input url here: ")
path = input("Enter path here: ")

video(url,path)

我想从另一个名为 mymodule.py 的 python 脚本获取用户输入,以获取用于 url 和路径的 check_avail.py 输入。我在网上搜索过,但没有好的答案。 谁能帮帮我?

【问题讨论】:

  • 如果您正在导入 check_avail.py,那么您可能不想获得输入或致电 video(),因此您可能想重新访问您的 if __name__ == "__main__": 块。另请注意,目前您return video 这可能不是您想要做的。

标签: python youtube user-input pytube


【解决方案1】:

经过这么多搜索,我找到了答案。 如果您在 python 程序中创建一类方法并使用来自另一个 python 脚本的用户定义变量,则必须在该 python 脚本中创建一个对象,从该位置导入它。

from pytube import YouTube
def video(url,path):
    y = YouTube(url) 
    video1 = YouTube(url).streams.first().download(path)
    print("Success") 
    return video  


url = input("Enter input url here: ")
path = input("Enter path here: ")

<---- Create a object here so that you can import and use anywhere-->
t = video(url,path)

script2.py

from pytube import YouTube
import pytube
# import check_avail 

class vdodownload:

<-- accessing it from another script and initialise to another variable-->
    def __init__(self,url,path):
        self.url = t.url
        self.path = t.path

<-- or you can directly use it here by giving t.url -->
    def link(self):
        try:
            print(f'Downloading video: {YouTube(t.url).title}')
            # yt = check_avail.video(t.url,t.path)
            YouTube(t.url).streams.first().download(t.path)
            print(f'Downloaded video: {YouTube(t.url).title}')

这是一种方法。如果需要任何更改,请更正它。

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2017-11-25
    • 2014-08-08
    相关资源
    最近更新 更多