# python期末项目 **Repository Path**: ai_learn/python-final-project ## Basic Information - **Project Name**: python期末项目 - **Description**: 基于python的Flask Web APP :binyun星座运势占卜平台 - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-08-14 - **Last Updated**: 2021-08-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # [binyun](http://120.78.178.247/) # Python 语言基础项目技术文档 ## 一、项目介绍 ### 1. 项目标题与简介 **[基于python的Flask Web APP :binyun星座运势占卜平台](http://120.78.178.247/)** binyun(即搜索(bing)+运势(yun))是一款星座运势占卜平台,通过注册账号,登录网站系统查询每天的星座运势,以了解自己星座每天需要注意的情况,为广大星座爱好者提供切实具体的占卜指标,平台基于api开发,为用户提供更好的用户体验 ![图](https://gitee.com/zhangbingli/python-final-project/raw/master/static/01.jpg "在这里输入图片标题") ### 2. 问题表述 **用户画象**:相信星座,对美好事务有一定憧憬的16-25岁青年人群体。 **用户使用场景**:在学习、生活、工作之前,每天想看一下自己的星座运势,并查看别人的星座运势,给自己一种心理安慰,更好的投入学习和工作中去。 **用户任务**:查询自己的星座运势/查询他人的星座运势 **用户痛点**:每一天都过得浑浑噩噩的,想看看今日的星座运势是怎么样的,但却找不到可以查询的地方。 --- ## 二、解决方案 ### 1. 项目规划与知识点 本项目主要通过注册与登陆系统、星座运势查询、日志系统3个功能,并配合 Bootstrap 的统一样式模版及 HTML 、CSS 相关知识来完成,以下为其中涉及到的Python基础知识点: ![图](https://gitee.com/zhangbingli/python-final-project/raw/master/static/02.jpg "在这里输入图片标题") ### 2. 编程功能的基本描述 本项目主体功能有注册与登陆系统、图文上传与展示、图像分类。涉及: - 使用[bootstrap](https://www.bootcss.com/)搭建网站统一样式模板 ```html ``` - [HTML 表单](https://www.runoob.com/html/html-forms.html),让用户输入账号/图文信息数据 ```html
账号
密码

请先点击这里完成“注册”

{% with messages = get_flashed_messages() %} {% if messages %}
    {% for message in messages %}
  • {{ message }}
  • {% endfor %}
{% endif %} {% endwith %}
``` - [HTTP 请求](https://www.runoob.com/http/http-methods.html),用于数据的传递 ```python @app.route('/register', methods=['GET', 'POST']) ``` ```html ``` - [Flask 第三方模块](https://blog.csdn.net/bbwangj/article/details/104646932)的使用 ```python from flask import Flask, render_template, request, escape,flash import json, urllib from urllib import parse ``` - [条件判断语句](https://www.runoob.com/python/python-if-statement.html),对比用户输入的信息和文件数据库里已经储存的信息或判断用户的登陆状态 ```python @app.route('/login',methods =['POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] if not username: flash("username empty") return render_template('login.html') if not password: flash('password empty') return render_template('login.html') account =[]; with open('user.txt','r') as r: data = r.readlines() if not data: flash('user empty') return render_template('register.html') print(account) for i in data: account.append(i.strip('\n').strip('\r\n')) flag =False if not account: flash('user empty') return render_template('register.html') for i in account: splitData= i.split(':') print(splitData) print(splitData[0]) print('username') print(username) if splitData: if username == splitData[0] and password ==splitData[1]: print('xxx') flag=True break print(flag) print('flag') if not flag: flash('username or password error') return render_template('login.html') return render_template('lists.html') ``` - [for 循环语句](https://www.runoob.com/python/python-for-loop.html),提取日志文件数据进行前端渲染 ```html {% for row_title in the_row_titles %} {% endfor %} {% for log_row in the_data %} {% for item in log_row %} {% endfor %} {% endfor %}
{{row_title}}
{{item}}
``` 查询需求功能,涉及: 聚合数据[星座查询api](https://www.juhe.cn/docs/api/id/58),十二星座每日、每月、每年运势 ```python def get_constellation(consName): key = "39afabac247a25466f47364bbe3ec9b1" payload = { 'consName': consName, 'type': 'today', 'key':key } req = requests.get('http://web.juhe.cn:8080/constellation/getAll', params=payload) return req.json() ``` 由于代码较多,后期需要对项目结构进行优化,对代码进行拆分与优化。涉及: - 自定义[模块](https://www.runoob.com/python/python-modules.html)的使用 ```python from vsearch import search4letters ``` - 日志系统存入数据,输出数据函数 ```python def log_request(req: 'flask_request', res: str) -> None: """Log details of the web request and the results.""" with open('vsearch.log', 'a') as log: print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|') ``` 以上各步骤均会遇到各类 Bug ,解决 Bug 需要: - 查看[报错信息](https://cloud.tencent.com/developer/article/1654527) - 在搜索引擎中搜寻解决方案 | 知识点 | HTML表单、HTTP 请求 | 文件处理 | 条件判断语句、 for 循环语句 | API、字典、列表 | 函数的封装 | Flask 第三方模块的使用 | | --- | --- | --- | --- | --- | --- | --- | | 学习成本(天) | 1 | 2 | 2 | 5 | 2 | 6 | | 应用比例 | 20% | 30% | 10% | 15% | 5% | 20% | ### 3. 云端项目部署的基本描述 #### 3.1 页面链接与页面功能介绍 **网站基本内容展示功能组** - 欢迎页:[http://120.78.178.247/login](http://120.78.178.247/login) - 日志系统管理页:[http://120.78.178.247/showlog](http://120.78.178.247/showlog) **注册与登陆系统功能组** - 注册页:[http://120.78.178.247/register](http://120.78.178.247/register) - 注册成功页:[http://120.78.178.247/register](http://120.78.178.247/register) (路由不变,form表单变化) - 登陆页:[http://120.78.178.247/](http://120.78.178.247/) **api调用输入输出功能组** - 输入查询页:[http://120.78.178.247/viewlog](http://120.78.178.247/viewlog) - 查询成功页:[http://120.78.178.247/search4](http://120.78.178.247/search4) 有效功能页面数量共7个 #### 3.2 云端功能 注册与登陆、星座查询api调用、账号信息与查询信息储存(文件数据库) #### 3.3 部署心得 在本地运行良好的项目在部署后并不能立刻良好地运行。部署初期我遇到了很多问题: 1. 云端服务器的[虚拟环境](https://www.jianshu.com/p/5d120cfd386e)没有配置、[模块未安装](https://blog.csdn.net/Simon0529/article/details/109329599); 1. 引用文件的路径使用了[相对路径而非绝对路径](https://www.w3cschool.cn/feijishu/je4c1px7.html),导致[文件不能读取或写入](https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/); 1. API接口未进入[网站的白名单](https://help.pythonanywhere.com/pages/RequestingWhitelistAdditions/),导致使用不了相关api功能 后面使用自行购买云服务器进行搭建(加分项): 1. 购买[阿里云云服务器](https://www.aliyun.com/activity?spm=5176.12901015.0.i12901015.16c0525c2j3q4w&accounttraceid=fd74d66dc6a243f190f3d7eb8cc9c445edpt) 1. 使用轻量级环境包miniconda 搭建python环境 1. 与本地部署相同将端口改为一下内容 ```python if __name__ == '__main__': app.run(host='0.0.0.0',port=80) ``` --- ## 三、学习/实践心得总结及感谢 第一次接触Python的时候还是很迷茫无措的,作为一名转学生,前期完全没有代码基础,学习起来特别的困难,所以单靠上课听讲是远远不够的,往往也是需要课外的学习与联系,借鉴与模仿。 “编程思维”是“理解问题——找出路径”的思维过程,此次的项目,代码、文件的增加,给我带来了许多挑战,一点小失误就会导致运行失败,在此过程中,多次的运行失败,但最后都能通过编程思维去理性分析找出问题,解决问题。 --- 独特有效外连URL:18个