开一个Python项目网络实践贴贴吧
本帖最后由 itubay 于 2024-6-19 20:04 编辑现在Python很流行,有网络交流的没有:handshake
Python中文指南
#Python项目源码hello.py
print("Hello,Python")
#命令行执行 python hello.py,前提是你要安装Python环境
#windows安装包
在线Python,打开WebShell直接敲代码
https://www.python.org/shell/
#web hello,wh.py
from flask import Flask
app=Flask(__name__)
@app.route("/")
def hello_world():
return "Hello World!"
if __name__=="__main__":
app.run()
#命令行执行python wh.py,然后打开指定网址
#getwip.py,获取公网ip
import requests
gip = requests.get("http://myip.ipip.net",timeout=5).text
print(gip)
#命令行执行python getwip.py即可 #listsyspath.py,列出系统目录
import os,sys
spath=sys.path
for path in sys.path:
if os.path.isfile(path):
print(path)
if os.path.isdir(path):
for file inos.listdir(path):
print(file)
#命令行执行 python listsyspath.py
#time.py,查询当前时间
import time
print("返回当前时间戳:",time.time())
print("返回当前时间:",time.ctime())
print("返回当前时间:",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("返回当前时间:",time.strftime("%Y-%m-%d %X", time.localtime()))
#命令行执行 python time.py
#getmtime.py
import time
import calendar
dt=time.localtime(time.time())
print("当前时间:",time.strftime("%Y-%m-%d %X", dt))
_,days=calendar.monthrange(dt.tm_year,dt.tm_mon)
print("本月天数:",days)
#命令行执行 python getmtime.py
#pystumis.py,学生管理系统
#!/usr/bin/python3
# coding=utf-8
"""使用python创建学生管理系统"""
""" 1.Python函数式编程实现思路 """
""" 使用字典把学生的姓名、qq、微信进行封装 """
""" 使用列表(list)装学生的信息 """
""" 实现对列表中字典数据进行增、删、改、查 """
import random
class StudentsSys(object):
def __init__(self):
self.names = []
self.info = "1:新增|2:删除|3:修改|4:查找|5:显示|6:退出系统"
def print_menu(self):
"""学生管理系统菜单"""
print("="*50)
print("\t学生管理系统菜单")
print("\t1:添加学生信息")
print("\t2:删除学生信息")
print("\t3:修改学生信息")
print("\t4:查找学生信息")
print("\t5:显示学生信息")
print("\t6:退出学生管理系统")
print("=" * 50)
def caozuo_zhiling(self):
print("\n")
print("~"*50)
print(self.info)
print("~"*50)
def addInfo(self):
self.students = {}
self.students['name'] = input("请输入学生姓名:")
for name in self.names:
if self.students["name"] == name["name"]:
print("姓名重复,请重新输入!")
self.start()
self.students['phone'] = input("请输入学生的手机号:")
if len(self.students['phone']) != 11:
print("请输入正确格式的手机号!")
return
self.students['email'] = input("请输入学生的邮箱:")
self.names.append(self.students)
#print(self.names)
self.saveInfo()
self.showInfo()
def delInfo(self):
def_name = input("请输入您要删除的学生姓名:")
print("您确定要删除该学生信息吗?删除属于不可逆操作,请您仔细考虑!")
msg = input("确认删除,请按'y',返回请按'n'")
if msg == "y":
for name in self.names:
if def_name == name.get("name"):
self.names.remove(name)
self.saveInfo()
self.showInfo()
else:
self.showInfo()
def modifyInfo(self):
modify_name = input("请输入您要修改的学生姓名:")
flag = 0
msg = input("确认修改,请按'y',返回请按'n'")
if msg == "y":
for name in self.names:
if modify_name == name["name"]:
new_name = input("请输入新的名字:")
name["name"] = new_name
flag = 1
self.showInfo()
if flag == 0:
print("该名学生%s不存在" % modify_name)
self.showInfo()
else:
self.showInfo()
def findInfo(self):
find_name = input("请输入你要查找的姓名:")
flag = 0
for name in self.names:
for value in name.values():
if find_name == value:
flag = 1
print("找到了:{}".format(find_name))
break
if flag == 0:
print("没有找到:{}".format(find_name))
def loadInfo(self):
file = open("students.txt","a+")
file.seek(0,0)
content = file.read()
if len(content) > 0:
self.names = eval(content)
def saveInfo(self):
file = open("students.txt","w")
file.write(str(self.names))
file.close()
def showInfo(self):
print("\n")
print("\t当前学生系统信息")
print("~"*50)
print("\t姓名\t\t手机号\t\t邮箱")
for name in self.names:
msg = "\t"+name.get("name")+"\t\t"+name.get("phone")+"\t"+name.get("email")
print(msg)
print("~"*50)
def numberIsdigit(self):
number = input("请输入指令:")
if number.isdigit():
number = int(number)
if number == 1:
self.addInfo()
elif number == 2:
self.delInfo()
elif number == 3:
self.modifyInfo()
elif number == 4:
self.findInfo()
elif number == 5:
self.showInfo()
elif number == 6:
msg = input("您确定要退出学生管理系统吗?,如果您确定退出请输入'y',继续使用请输入'n':")
if msg == "y":
print("成功退出学生管理系统,欢迎您的下次使用!")
if msg == "n":
self.start()
else:
print("请输入正确的指令,指令为1-6的纯数字!")
def start(self):
self.print_menu()
self.loadInfo()
self.caozuo_zhiling()
self.numberIsdigit()
s = StudentsSys()
s.start()
#命令行执行 python pystumis.py
#pywsh.py,上海空气质量报告
import requests
from lxml import etree
url = "https://data.rmtc.org.cn/gis/listsation0_94M.html"
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"
with requests.request('GET',url,headers = {'User-agent':ua}) as res:
content = res.text
#print(content)
html = etree.HTML(content)
shdiv = html.xpath("//div[@class='main']//text()")
for i in shdiv:
s=i.strip()
if len(s)>0:print(s)
divnames = html.xpath("//div[@class='main']//li[@class='datali']//div[@class='divname']//text()")
divvals = html.xpath("//div[@class='main']//li[@class='datali']//span[@class='label']//text()")
divdates = html.xpath("//div[@class='main']//li[@class='datali']//span[@class='showtime']//text()")
sdivnames=[]
sk=0
for i in divnames:
s=i.strip()
if len(s)>0:
sk+=1
sdivnames.append(s)
ans = dict(zip(sdivnames,divvals))
sk=0
print("-"*25)
for k,v in ans.items():
k=k.strip()
v=v.strip()
print(k,v,divdates)
sk+=1
print("-"*25)
for i in range(4):
print(sdivnames.strip(),divvals.strip(),divdates.strip())
#命令行执行 python pywsh.py
页:
[1]
2