项目1 Flask Web框架应用案例
一、Flask 介绍
- 1. 介绍Flask
- Flask 是一个轻量级的 Python Web 框架,由 Armin Ronacher 在 2010 年创建。它被设计成简单易用、灵活可扩展的框架,旨在帮助开发者快速构建 Web 应用程序。相比于其他框架,如 Django,Flask 更加轻量级,它提供了基本的功能和结构,但允许开发者根据需求自由选择和集成其他库和工具。
二、项目介绍
- 你是某公司程序员,公司正在做安全分析人工智能,需要大量网上样品做分析训练,公司委派你来做这个程序。
- 样品图片网站: https://www.doutupk.com/
三、功能需求
- 功能需求:
- 1. 使用Python3编写代码,将样品链接全部获取。
- 2. 使用Python3中的Flask Web框架,展示样品链接HTML
- 3. 使用Python3中的Flask Web框架,编写搜索功能,接收Get请求变量,获取详细样品链接
四、实现思路
4.1 安装相关第三方库
- 打开cmd输入
- pip3 install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
- pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple
4.2 部署Flask Web框架
- # 导入第三库
- from flask import Flask
-
- # 创建Flask应用
- app = Flask(__name__)
-
- # 定义路由和视图函数
- @app.route('/')
- def index():
- return "Hello, World"
-
- # 运行应用
- if __name__ == '__main__':
- app.run(host="0.0.0.0", port=9090, debug=True)
4.3 实现首页路由
- # 在Web框架代码新增以下参数
- # 导入第三库
- from flask import Flask,render_template
-
- @app.route('/')
- def index():
- return render_template("index.html")
index.html 首页
- # 在项目文件夹中,创建 templates 目录
- # 在templates目录,创建首页 index.html 代码如下:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <style>
- body, html {
- height: 100%; /* 设置html和body的高度为100% */
- margin: 0; /* 去掉默认的边距 */
- }
-
- .full-background {
- background-image: url('https://static.iamxk.com/wp-content/uploads/2019/04/dc7f6d71-e155-40cc-8074-1fb927bbfbec-2.jpg'); /* 设置背景图 */
- height: 100%; /* 容器高度100% */
- width: 100%; /* 容器宽度100% */
- background-position: center; /* 背景图片居中显示 */
- background-repeat: no-repeat; /* 背景图片不重复 */
- background-size: cover; /* 背景图片覆盖整个容器 */
- position: absolute; /* 使用绝对定位 */
- }
- </style>
- <body>
- <div class="full-background"></div>
- </body>
- </html>
因首页HTML待开发,故使用404图片代替
4.4 实现搜索以及样品展示页面
- # 在Web框架代码新增以下参数
- # 导入第三库
- from flask import Flask,render_template, request
-
- @app.route('/img')
- def img():
- # 接收name参数
- name = request.args.get('name')
- # 判断name值name值是否为空
- if name:
- # 样品url拼接name参数
- url = f"https://www.pkdoutu.com/search?keyword={name}"
- # 模拟请求获取响应
- html = requests.get(url, headers=headers).text
- # 构造正则匹配规则
- # pattern = r'data-original="([^"]+)"'
- pattern = r'data-original="(.*?)"'
- # 使用正则匹配响应内容中的信息
- matches = re.findall(pattern, html)
- # 将查找到的参数,返回img.html页面
- return render_template('img.html', matches=matches)
- else:
- # 为空返回404页面
- return render_template("index.html")
img.html 样品展示页面
- # 在templates目录,创建样品展示页面 img.html 代码如下:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Flask Template Example</title>
- </head>
- <style>
- .box {
- width: 100%;
- margin: 0 auto;
- }
- @media screen and (max-width: 1080px) {
- img {
- height: 125px;
- }
- }
- </style>
- <body>
-
- <div class="box">
- {% for value in matches %}
- <img src="{{ value }}" width="200px" height="200px">
- {% endfor %}
- </div>
-
- </body>
- </html>
样品展示页面 效果
完整代码
- # 导入第三库
- import requests
- import re
- from flask import Flask, render_template, request
-
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"
- }
-
-
- # 创建Flask应用
- app = Flask(__name__)
-
- # 定义路由和视图函数
- @app.route('/')
- def index():
- return render_template("index.html")
-
- @app.route('/img')
- def img():
- # 接收name
- name = request.args.get('name')
- # 判断name值name值是否为空
- if name:
- url = f"https://www.pkdoutu.com/search?keyword={name}"
- html = requests.get(url, headers=headers).text
- pattern = r'data-original="([^"]+)"'
- matches = re.findall(pattern, html)
- return render_template('img.html', matches=matches)
- else:
- return render_template("index.html")
-
- # 运行应用
- if __name__ == '__main__':
- app.run(host="0.0.0.0", port=9090, debug=True)
-
来源:默认网盘
原创文章,作者:智企网络工作室,如若转载,请注明出处:https://www.harcker01.cn/index.php/archives/15/