Flask表单request.form.get('url')返回None问题求助
问题:Flask无法获取表单中的YouTube URL
使用Flask+HTML开发YouTube下载工具,提交表单后request.form.get('url')返回None,无法获取输入的视频URL。以下是相关代码,怀疑问题出在HTML表单中。
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>YouTube Download</title> <link rel="stylesheet" href="../static/css/style.css"> <link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <div class="banner"> <h1 class="title" data-text="Youtube Download"><i class="fa fa-music"></i> Youtube Download</h1> <form action="{{ url_for('downloadMP3') }}" method="POST"> <p>Ingresa la URL del Video </p> <br> <input class="form-control" type="text" id="url" name=“url” > <div class="btn-group"> <button class="btn" type="submit" formaction="/downloadMP4"> <i class="fa fa-download"> </i> Download MP4 </button> <button class="btn" type="submit" formaction="/downloadMP3"> <i class="fa fa-download"> </i> Download MP3 </button> </div> </form> </div> </body> </html>
Python代码
from flask import Flask, render_template, request, Response, redirect, send_file import os from os import remove import pafy import moviepy.editor as mp app = Flask(__name__) path=os.getcwd() + '/' @app.route('/') def route(): return render_template('index.html') @app.route('/downloadMP4', methods=['GET', 'POST']) def downloadMP4(): if request.method == 'POST': url = request.form.get("url") video = pafy.new(url) best = video.getbest(preftype="mp4") best.download(path) p = path + video.title + '.mp4' return send_file(p, as_attachment=True) @app.route('/downloadMP3', methods=['GET', 'POST']) def downloadMP3(): if request.method == 'POST': url = request.form.get("url") video = pafy.new(url) best = video.getbest(preftype="mp4") best.download(path) name = path + video.title + '.mp4' clip = mp.VideoClip(name) clip.audio.write_audiofile(path + video.title + '.mp3') p = path + video.title + '.mp3' return send_file(p, as_attachment=True) if __name__ == '__main__': app.run(host='localhost', port=5000)
解决方案
核心问题:HTML表单的引号错误
HTML中input标签的name属性使用了中文双引号(“url”),而非标准的英文双引号("url"),导致Flask无法识别表单字段,返回None。
修正后的input标签:
<input class="form-control" type="text" id="url" name="url" >
额外优化建议
- 移除重复的静态资源引用:HTML头部重复引入了
style.css,保留Flask的url_for方式即可,避免路径问题:
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
- 处理异常情况:Python代码中未对
url为空、视频下载失败等情况做处理,容易报错,可添加判断:
@app.route('/downloadMP4', methods=['GET', 'POST']) def downloadMP4(): if request.method == 'POST': url = request.form.get("url") if not url: return "请输入有效的YouTube URL", 400 try: video = pafy.new(url) best = video.getbest(preftype="mp4") best.download(path) p = path + video.title + '.mp4' return send_file(p, as_attachment=True) except Exception as e: return f"下载失败:{str(e)}", 500 return redirect('/')
- 清理临时文件:下载完成后可删除临时的MP4文件(尤其是转MP3的场景),避免占用磁盘空间:
# 在MP3下载的函数中,转码完成后删除原MP4文件 os.remove(name)
内容的提问来源于stack exchange,提问作者Ezequiel Bonasegla
相关产品推荐
相关产品推荐

