Python Flask: 用户下载爬取的图像

2024-06-16 11:53:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小脚本,这是一个图像刮刀。基本上,您为脚本提供了一个链接,它会将网页上的所有图像下载到您桌面上的一个文件夹中。我想要相同的功能,但在浏览器上。我让用户输入一个链接,然后脚本开始从链接下载图像到他们的计算机上。我的代码如下:

@app.route('/')
def main():
   return render_template('index.html')

@app.route('/Downlading', methods=['POST'])
def Downlading():
   url= request.form['url']
   start = request.form['start']
   end = request.form['end']
   dphy = image_downloader(url) #this is the script that saves the images
   return str(dphy)

我可以得到用户的网址,并把它传递给图片下载程序,下载图片。问题是图像是从命令提示符下载的。我希望脚本在浏览器中运行,就像它在我的IDE中运行一样。抱歉,如果这让人困惑。你知道吗

我的HTML代码:

    <form action="/Downlading" method="POST" >
        URL: <input type="text" name="url"><br/>
        Start: <input type="text" name="start"><br/>
        End: <input type="text" name="end"><br/>
        <input type="submit" name="form" value="Submit" />

    </form>

Tags: textname图像brform脚本urlinput
1条回答
网友
1楼 · 发布于 2024-06-16 11:53:15

您需要为要反映的变量创建一个HTML模板。例如:

HTML-上传.html地址:

<html>
<title>Example on StackOverflow</title>
<p> The str representation of the variable dphy is {{ dphy }} </p>
</html>

Python(将其添加到现有的flask脚本中):

@app.route('/Downlading', methods=['POST'])
def Downlading():
   url= request.form['url']
   start = request.form['start']
   end = request.form['end']
   dphy = image_downloader(url) #this is the script that saves the images
   return render_template('upload.html', dphy=str(dphy))

这应该管用,但我现在不能测试,所以我不是阳性。这是通过Flask传递变量的基本思想—创建一个使用该变量的模板,然后在呈现创建的模板时显式地传递它。你知道吗

相关问题 更多 >