02.Python百行代码实现抽奖系统
2.1 服务访问
# 1 电脑可以支持服务访问 # 需要一个 web 框架 Flask # pip install Flask from flask import Flask # 导入框架 app = Flask(__name__) # 基于 Flask 类创建对象 app @app.route('/index') # 设置访问路径资源 def index(): return f'Hello World!' # 访问路径返回的内容 app.run(debug=True) # 运行框架,调试 bug ------------------------------------------------ 执行后 C:\Users\马俊南\AppData\Local\Programs\Python\Python37\python.exe D:\Pycharm\code\02python项目实战\02Python百行代码实现抽奖系统\01Python百行代码实现抽奖系统.py * Serving Flask app '01Python百行代码实现抽奖系统' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit * Restarting with stat * Debugger is active! * Debugger PIN: 128-338-529
根据提示本地浏览器访问 5000 端口,可以通过本地访问
2.1 设置访问
2.2.1 设置英雄
# 2.2 网址: https://lol.qq.com/data/info-heros.shtml 使用 XPath 提取: //ul/li/a/p hero = ['黑暗之女','狂战士','正义巨像','卡牌大师','德邦总管','无畏战车','诡术妖姬','猩红收割者', '远古恐惧','正义天使','无极剑圣','牛头酋长','符文法师','亡灵战神','战争女神','星之子']
2.2.2 展示英雄
创建 templates 目录,在此目录下创建 index.html 文件
<!-- templates 目录下 index.html 文件 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 此处是标题 --> </head> <body> {{ hero_html }} <!-- 此处是页面展示内容, 引用前面赋值给 hero_html 的内容! --> </body> </html>
# 1 电脑可以支持服务访问 # 需要一个 web 框架 Flask # pip install Flask from flask import Flask,render_template # 导入框架 app = Flask(__name__) # 基于 Flask 类创建对象 app # 网址: https://lol.qq.com/data/info-heros.shtml 使用 XPath 提取格式: //ul/li/a/p hero = ['黑暗之女','狂战士','正义巨像','卡牌大师','德邦总管','无畏战车','诡术妖姬','猩红收割者', '远古恐惧','正义天使','无极剑圣','牛头酋长','符文法师','亡灵战神','战争女神','星之子'] @app.route('/index') # 设置访问路径资源 访问 http://127.0.0.1:5000/index def index(): return render_template('index.html',hero_html = hero) # 访问 /index 返回 index.html 文件内容, hero 列表内容 赋值给 hero_html app.run(debug=True) # 运行框架, debug 支持热加载, 更改index.html内容不需要重新再启动程序
使用浏览器访问:http://127.0.0.1:5000/index
2.2.3 抽奖按钮
------------------------------------------------ 执行后
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习