提交 28a0c449 编写于 作者: 梦想橡皮擦's avatar 梦想橡皮擦 💬

登录采集

上级 c738b98b
.idea/
venv/
app/__pycache__/
\ No newline at end of file
__pycache__/
......@@ -5,7 +5,7 @@ from .config import BaseConfig # 导入配置文件
# Flask 限流器
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address, get_ipaddr
# from flask_limiter.util import get_remote_address, get_ipaddr
# 导入过滤器
from .filter_fun import datauri
......@@ -14,6 +14,7 @@ app = Flask(__name__)
app.config.from_object(BaseConfig) # 启用配置
app.jinja_env.filters['datauri'] = datauri
app.secret_key = 'xiangpica'
def get_real_ip():
......
from flask_wtf import FlaskForm
from wtforms.validators import DataRequired, Length
from wtforms import StringField, SubmitField, PasswordField
# 定义的表单都需要继承自FlaskForm
class UserForm(FlaskForm):
username = StringField('用户名', validators=[DataRequired(), Length(3, 15)])
password = PasswordField('密码', validators=[DataRequired(), Length(5, 15)])
submit = SubmitField('登录')
\ No newline at end of file
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import db
class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True)
password_hash = db.Column(db.String(128))
@property
def password(self):
# 设置密码的属性为只写
raise AttributeError('密码不能读取')
@password.setter
def password(self, password):
# 当类初始化时自动调用,把 password 的值转换为 hash 值
self.password_hash = generate_password_hash(password)
def verify_password(self, password) -> bool:
# 登录时验证密码
return check_password_hash(self.password_hash, password)
from flask import render_template, make_response
from flask import render_template, make_response, request, redirect, url_for, flash
from app import app
import hashlib
"""
# 临时处理插入一下数据
from sqlalchemy.orm import sessionmaker
from app import db
"""
# 用户权限认证相关数据
from app.login_form import UserForm
from app.login_model import User
from flask_login import LoginManager,login_user,login_required,logout_user
# use login manager to manage session
login_manager = LoginManager(app)
login_manager.login_view = 'login' # 设置登录页面
# 回调函数,用来加载用户
@login_manager.user_loader #
def load_user(id):
'''用于加载用户'''
return User.query.get(int(id))
"""
首页相关路由配置
"""
......@@ -52,3 +76,43 @@ def timeline():
nav = dict()
nav['time'] = 'active'
return render_template('timeline.html', nav=nav)
@app.route("/login", methods=['GET', 'POST'])
def login():
form = UserForm()
print(form.validate_on_submit())
"""
# 创建session类型
DBSession = sessionmaker(bind=db.engine)
# 创建session对象
session = DBSession()
# 创建新的user对象
new_user = User()
new_user.username = 'xiangpica'
new_user.password = '123456'
session.add(new_user)
# 提交即保存到数据库
session.commit()
# 关闭session
session.close()
"""
if form.validate_on_submit():
# 查询用户信息
user = User.query.filter_by(username=form.username.data).first()
if user is not None:
if user.verify_password(form.password.data):
flash('登录成功')
login_user(user) # login_user 的参数为要登录的用户
return redirect(request.args.get('next') or url_for('school.login_list_school'))
flash('登录失败')
return render_template('login.html', form=form)
# 退出登录
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
\ No newline at end of file
......@@ -10,6 +10,8 @@ from ..model import School # 导入上级模块
# 从 app 中导入 limiter 对象
from app import limiter
# 导入 flask_login 内部函数
from flask_login import login_required
s = Blueprint('school', __name__, url_prefix='/ss')
......@@ -82,6 +84,15 @@ def list_school():
pagination = pagination_object(page)
return render_template('school/index.html', pagination=pagination)
# 登录后可以查阅
@s.route('/login_list')
@login_required
def login_list_school():
page = int(request.args.get("page", 1)) # 获取页码
pagination = pagination_object(page)
return render_template('school/login_index.html', pagination=pagination)
@s.route('ajax_list')
def ajax_list():
......
......@@ -105,9 +105,7 @@
<div class="card border-info rounded-5 shadow-sm" style="min-height:306px;min-width:300px;">
<div class="card-header text-center">
<h4 class="card-title">慢速爬虫</h4>
<div class="bg-danger text-white rounded p-1"
style="transform: rotate(20deg); position:absolute;right:0;top:0.5rem;">最新更新
</div>
</div>
<div class="card-body">
<p class="card-text">本案例通过控制请求响应速度,来实现慢速爬虫,编写采集程序,需要控制请求和响应时间。</p>
......@@ -147,7 +145,29 @@
</div>
</div>
</div>
<div class="col mt-2">
<div class="card border-info rounded-5 shadow-sm" style="min-height:306px;min-width:300px;">
<div class="card-header text-center">
<h4 class="card-title">登录加载数据</h4>
<div class="bg-danger text-white rounded p-1"
style="transform: rotate(20deg); position:absolute;right:0;top:0.5rem;">最新更新
</div>
</div>
<div class="card-body">
<p class="card-text">本案例需模拟登录,然后才可采集数据,实践中可分析登录之后系统在客户端存储的用户状态信息。</p>
<p class="card-text text-left">难度:⭐⭐</p>
<p class="card-text">
案例:
<a href="/ss/login_list" class="card-link text-success">学校清单</a>
</p>
</div>
<div class="card-footer text-end">
<a href="https://blog.csdn.net/hihell/article/details/128474916" target="_blank"
class="card-link text-muted small">案例制作教程</a>
<a href="#" class="btn btn-success btn-sm card-link disabled" alt="暂未开放">学习博客</a>
</div>
</div>
</div>
</div>
</div>
......@@ -285,7 +305,7 @@
</p>
</div>
<div class="card-footer text-end">
<a href="https://dream.blog.csdn.net/article/details/128474930" target="_blank"
<a href="https://dream.blog.csdn.net/article/details/128550667" target="_blank"
class="card-link text-muted small">案例制作教程</a>
<a href="#" class="btn btn-success btn-sm card-link disabled" alt="暂未开放">学习博客</a>
</div>
......
{% extends "base.html" %}
{% block style %}
<style type="text/css">
.required:after{
content: "*";
color: red;
}
</style>
{% endblock style %}
{% block script %}
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
{% endblock script %}
{% block content %}
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6 mt-5 mb-5 p-5 border border-success shadow-lg ">
<h1 class="text-primary">登录窗口</h1>
<form action="{{ url_for('login') }}" method="POST" class="mt-4">
<div class="form-group">
<span class="required"> {{ form.username.label }} </span>
{{form.username(class="form-control",placeholder="请输入用户名",data_toggle="tooltip",title="测试账号:xiangpica")
}}
<small id="username_help" class="form-text text-muted">用户名至少3个字符</small>
</div>
<div class="form-group">
<span class="required"> {{ form.password.label }} </span>
{{ form.password(class="form-control",placeholder="请输入密码",data_toggle="tooltip",title="测试密码:123456") }} <br>
</div>
<div class="form-group">
{{ form.submit(class="btn btn-primary" ) }}
</div>
{{ form.csrf_token }}
</form>
</div>
</div>
</div>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% block style %}
<style>
.logout{
width:100%;
}
</style>
{% endblock style %}
{% block content %}
<div class="container">
{% for school in pagination.data_list %}
<div class="row mt-3">
<div class="col">
<div class="d-flex">
<div class="flex-shrink-0">
<a href="#">
<img class="rounded-pill img-thumbnail" width="64" height="64" src="{{school.pic}}" alt="">
</a>
</div>
<div class="flex-grow-1 ms-3">
<h5 class="float-start pe-3">{{school.name}}</h5>
<p class="ms-3">
{% for fea in school.feature.split(',') %}
<span class="badge rounded-pill bg-primary">{{fea}}</span>
{% endfor %}
</p>
<p><em>所在省市:<span class="text-black-50">{{school.province}} -- {{school.city}}</span></em></p>
</div>
</div>
</div>
</div>
{% endfor %}
<a class="btn btn-danger mt-2 logout" href="/logout" >点击退出登录</a>
</div>
{% endblock %}
/*
Navicat Premium Data Transfer
Source Server : a
Source Server Type : MySQL
Source Server Version : 50553
Source Host : localhost:3306
Source Schema : playground
Target Server Type : MySQL
Target Server Version : 50553
File Encoding : 65001
Date: 11/01/2023 16:22:54
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password_hash` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, 'xiangpica', 'pbkdf2:sha256:260000$YXBcAzNRmppCHIpd$21e3e35e2aa85f9750fb33b3fc9a387afc6f38ade43366b35042487b2d93aa3b');
SET FOREIGN_KEY_CHECKS = 1;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册