Search code examples
pythonflaskjinja2pytube

I'm getting a (TypeError: expected string or bytes-like object) while working with pytube library


python

from flask import Flask, request, render_template
from pytube import YouTube
import pytube

app = Flask(__name__)


@app.route("/", methods=["POST", "GET"])
def find():
    search1 = request.form.get("search")
    print(search1)
    print(type(search1))
    subt = pytube.YouTube(search1)  # <-- Error occurs in this line.
    print(subt)
    cap = subt.captions.get_by_language_code('en')
    print("Video english subtitle")
    print(cap.generate_srt_captions())
    return render_template("sub.html")


if __name__ == "__main__":
    app.run(debug=True)

Error: TypeError: expected string or bytes-like object

results = regex.search(string)

TypeError: expected string or bytes-like object


Solution

  • from flask import Flask, request, render_template
    from pytube import YouTube
    import pytube
    
    app = Flask(__name__)
    
    @app.route("/", methods=["GET"])
    def index():
        return render_template("sub.html")
    
    
    @app.route("/test", methods=["POST"])
    def find():
        search1 = request.form.get("search")
        print(search1)
        print(type(search1))
        subt = pytube.YouTube(search1)
        cap = subt.captions.get_by_language_code('en')
        print("Video english subtitle")
        print(cap.generate_srt_captions())
        return render_template("sub.html", cap=cap)
    
    
    if __name__ == "__main__":
        app.run(debug=True)
    

    You can use this code to download the youtube subtitle.