主文件:total_demo.py
#模块<==>python文件
#从kuaidi_demo模块中引入query_kd函数
# from kuaidi_demo import query_kd
# 从kuaidi_class模块中引入KD类
from kuaidi_class import KD
# 从waether_demo中引入weather函数
# from weather_demo import weather
from weather_class import Weather
# 从student_demo中引入 student_manager函数
# from student_function_demo import student_manger
#若直接引入文件,在声明对象时,要写对象名=文件名.类( )
import student_class
import phone_class
from travel_class import Travel
from scenery_spot_class import Scenery
from NeiHanDuanZi_class import NeiHanDuanZi
from Mobile_home_class import Mobile_home
while 1:
print('1.快递查询')
print('2.天气查询')
print('3.学员管理')
print('4.手机销售')
print('5.旅游查询')
print('6.景点查询')
print('7.内涵段子')
print('8.手机归属地')
print('0.退出')
num=input('请选择(按0退出):')
if num=='1':
# query_kd()
#创建一个KD的对象
kd=KD()
kd.query_kd()
elif num=='2':
while 1:
city = input('请输入您要查询的城市名称(按0退出):')
if city == '0':
print('您已退出天气查询系统!')
break
weather = Weather(city)
weather.weather_query()
elif num=='3':
student = student_class.Student()
student.start()
elif num=='4':
phone =phone_class.Phone_Syetem()
phone.start()
elif num=='5':
travel = Travel()
travel.query()
elif num=='6':
scenery = Scenery()
scenery.query()
elif num=='7':
nhdz = NeiHanDuanZi()
nhdz.get()
elif num=='8':
mobile_home = Mobile_home()
mobile_home.query()
else:
break
快递查询:kuiadi_class.py
#快递查询
import requests
#从requests中只是引入get函数 调用时直接写get
# from requests import get
import json
#定义模块功能函数
#以类的形式写代码
class KD(object):
def __init__(self):
#存放公司代号的字典
self.kd_dict = {1: 'shentong', 2: 'ems', 3: 'shunfeng', 4: 'yuantong', 5: 'zhongtong', 6: 'yunda',
7: 'tiantian',
8: 'huitongkuaidi', 9: 'quanfengkuaidi', 10: 'debangwuliu', 11: 'zhaijisong'}
#查询快递的功能
def query_kd(self):
while True:
print('支持以下快递公司查询')
print('1.申通快递')
print('2.EMS邮政快递')
print('3.顺丰快递')
print('4.圆通快递')
print('5.中通快递')
print('6.韵达快递')
print('7.天天快递')
print('8.汇通快递')
print('9.全峰快递')
print('10.德邦物流')
print('11.宅急送')
print('0.退出')
num=int(input('选择您的快递公司:'))
while num not in range(0,12):
num=int(input('选项有误,请重选:'))
if num==0:
break
type=self.kd_dict[num]
postid=input('请输入您的快递单号:')
url='http://www.kuaidi100.com/query?type=%s&postid=%s'%(type,postid)
rs=requests.get(url)
rs_dict=json.loads(rs.text)
msg=rs_dict['message']
if msg=='ok':
data=rs_dict['data']
for msg in data:
#取出物流信息时间
time=msg['time']
context=msg['context']
print('时间:%s %s'%(time,context))
else:
if msg=='参数错误':
print('您输入的快递单号有误,请检查后重新输入!')
else:
print(msg)
#发快递
class F_KD(object):
'''发快递的功能..'''
pass
天气查询:weather_class.py
#天气预报
# url 统一资源定位符
#windows+r cmd 打开命令 输入pip install requests 回车
#api.map.baidu.com(百度服务器地址)
import requests
#引入python中内置的包
import json
class Weather(object):
def __init__(self,city):
self.city=city
def weather_query(self):
print('*************欢迎进入天气查询系统**************')
url='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak' \
'=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%self.city
#使用requests发送请求,接受返回的结果
response=requests.get(url)
# print(type(response.text))
#使用loads函数,将json字符串转换为python的字典或列表
rs_dict=json.loads(response.text)
#取出error
error_code=rs_dict['error']
#如果取出的error为0,表示数据正常,否则没有查询到天气信息
if error_code==0:
#从字典中取出数据
results=rs_dict['results']
#根据索引取出城市天气信息字典
info_dict=results[0]
#根据字典的key 取出城市名称
city_name=info_dict['currentCity']
pm25=info_dict['pm25']
print('当前城市:%s pm值:%s'%(city_name,pm25))
#取出天气信息列表
weather_data=info_dict['weather_data']
#for循环取出每一天天气的小字典
for weather_dict in weather_data:
#取出日期、天气、风级、温度
date=weather_dict['date']
weather=weather_dict['weather']
wind=weather_dict['wind']
temperature=weather_dict['temperature']
print('%s %s %s %s'%(date,weather,wind,temperature))
else:
print('没有查询到天气信息!')
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
while 1:
city = input('请输入您要查询的城市名称(汉字)(按0退出):')
if city == '0':
print('您已退出天气查询系统!')
break
weather=Weather(city)
weather.weather_query()
学员管理:student_class.py
#类 作为数据模型使用
# 初始化中给对象属性赋值
#数据模型类
class Student_demo(object):
def __init__(self,name,age,phone):
self.name=name
self.age=age
self.phone=phone
class Student(object):
#初始化中给对象属性赋值
# def __init__(self,name,age,phone):
# self.name=name
# self.age=age
# self.phone=phone
def __init__(self):
self.student_list = []
def add_student(self):
# 输入学员姓名、年龄、电话
self.name = input('请输入学员姓名:')
self.age = input('请输入学员年龄:')
self.phone = input('请输入学员电话:')
student=Student_demo(self.name,self.age,self.phone)
self.student_list.append(student)
print('添加学员成功!')
def query_student(self):
# 1.查询所有学员
# 2.输入学员姓名 查询学员得到查询的学员的序号
print('1.查询所有学员')
print('2.查询部分学员')
num = int(input('请输入操作序号:'))
while num not in range(1, 3):
num = int(input('选择无效,请重新输入:'))
if num == 1:
print('**************学员信息列表***************')
# 遍历大列表
for x in range(0, len(self.student_list)):
# 根据x的值从大列表中取出小字典
student = self.student_list[x]
name = student.name
age = student.age
phone = student.phone
print('序号:%s 姓名:%s 年龄:%s 电话:%s' % (x, name, age, phone))
else:
name = input('请输入您要查询的学员姓名:')
while 1:
a = False
#此处不能用泛型遍历,因为泛型遍历是根据内容进行删除的,当所有的名字都为a时,它只会匹配第一个
for x in range(0,len(self.student_list)):
if self.student_list[x].name == name:
print('序号:%s 姓名:%s 年龄:%s 电话:%s' % (
x, self.student_list[x].name, self.student_list[x].age,
self.student_list[x].phone))
a = True
if a == False:
name = input('该学员没找到,请重新输入:')
else:
break
# 修改学员的函数
def update_student(self):
# 判断是否有学员信息,如果没有,直接结束函数的执行
if len(self.student_list) == 0:
print('没有学员信息,无法进行修改操作!')
# 强制结束函数的执行 return下面的代码都不会再执行了
return
# 1.查询学员信息
self.query_student()
# 2.选择要修改的学员序号
num = input('请选择要修改的学员序号:')
# 3.转换为整数
num = int(num)
# 4.判断选择的学员序号是否在范围内
while num not in range(0, len(self.student_list)):
# 不在范围,重新选择
num = input('没有该序号,请重选:')
num = int(num)
# 5.根据选择的序号取出对应的小字典
student = self.student_list[num]
new_name = input('请输入修改后的姓名(%s):' % student.name)
new_age = input('请输入修改后的年龄(%s):' % student.age)
new_phone = input('请输入修改后的电话(%s):' % student.phone)
# 6.修改小列表中的数据
student.name = new_name
student.age = new_age
student.phone = new_phone
print('修改数据完成!')
# 删除学员
# 1.根据学员序号删除 2.删除所有学员 3.根据学员的姓名来删除(有同名的)
def delete_student(self):
if len(self.student_list) == 0:
print('没有学员信息,无法执行删除操作!')
return
print('1.根据学员序号删除')
print('2.删除所有学员')
print('3.根据学员姓名删除学员')
# 获取输入的内容并转换为整数类型
num = int(input('请输入您的选择:'))
# 判断选择的选项是否在范围内
while num not in range(1, 4):
num = int(input('没有该序号,请重新选择'))
# 判断选择的选项
if num == 1:
# 1.查询学员信息
self.query_student()
# 2.选择删除的序号
num = int(input('请输入您要删除的学员序号:'))
# 判断选择序号是否在范围内
while num not in range(0, len(self.student_list)):
num = int(input('序号无效,请重选!'))
is_del = input('您确定要删除(%s)学员的信息吗?(y/n):' % self.student_list[num].name)
if is_del == 'y':
# 删除列表中的所有数据
name=self.student_list[num].name
del self.student_list[num]
# student_list.pop(index)
print('%s学员信息删除成功!' %name )
else:
print('删除数据操作已去取消!')
elif num == 2:
# 确认删除
is_del = input('您确定要删除所有学员信息吗?y(确定)/n(取消):')
if is_del == 'y':
# 删除列表中的所有数据
self.student_list.clear()
print('所有学员删除成功!')
else:
print('删除数据操作已去取消!')
else:
name = input('请输入您要删除的学员的姓名:')
while 1:
# 定义列表存放不等于name的小列表
list = []
# 遍历大列表
for student in self.student_list:
# 判断输入的name是否和小字典里name的相等
if student.name != name:
# 找出与name不等的小字典所在的索引
index = self.student_list.index(student)
# 将符合的小字典添加到list列表中
list.append(self.student_list[index])
# 判断两个列表长度是否相等 相等说明大列表中不存在名字为name的小列表
if len(self.student_list) == len(list):
name = input('序号不存在,请重新输入:')
# 存在符合的小字典
else:
# 清空大列表
self.student_list.clear()
# 循环将list列表的内容写入到空的大列表中
for dict in list:
self.student_list.append(dict)
break
def start(self):
while 1:
print('***********学员管理系统V2.0**************')
print('1.添加学员')
print('2.查询学员')
print('3.修改学员')
print('4.删除学员')
print('0.退出程序')
print('***************************************')
num = int(input('请输入您的选择:'))
while num not in range(0, 5):
num = int(input('序号无效,请重输:'))
if num == 1:
self.add_student()
elif num == 2:
self.query_student()
elif num == 3:
self.update_student()
elif num == 4:
self.delete_student()
else:
break
#输出
# print(__name__)
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
student=Student()
student.start()
手机销售:phone_class.py
#数据模型类
class Phone(object):
def __init__(self,name,price,count):
self.name=name
self.price=price
self.count=count
class Phone_Syetem(object):
def __init__(self):
# 声明大列表,存储手机信息
self.phones_list = []
def query_phone(self,type):
'''
:param type: 查询时,输出的类型 1.输出详细信息(名称,价格,库存)2.输出产品名称
:return:
'''
for x in range(0,len(self.phones_list)):
#根据索引取出手机信息小字典
phone=self.phones_list[x]
name = phone.name
#判断输出的类型
if type==1:
price=phone.price
count=phone.count
#输出详细信息
print('序号:%s 产品名称:%s 产品价格:%s 产品库存:%s'%(x,name,price,count))
else:
print('序号:%s 产品名称:%s'%(x,name))
def buy_phone(self):
if len(self.phones_list)<=0:
print('当前无商品信息!')
return
print('1.选择序号查看手机详情')
print('2.返回')
num=int(input('请选择您的操作:'))
while num not in range(1,3):
num=int(input('选项有误,请重选:'))
if num==1:
#选择输入的产品序号
index=int(input('请输入查看的产品序号:'))
while index not in range(0,len(self.phones_list)):
index=int(input('序号有误,请重选'))
#根据index的值,取出小字典
phone=self.phones_list[index]
#
print('序号:%s 产品名称:%s 产品价格:%s 产品库存:%s'%(index,phone.name,phone.price,phone.count))
#是否购买
print('1.购买')
print('2.返回')
num=int(input('请选择:'))
while num not in range(1,3):
num=int(input('选择错误,请重选:'))
if num==1:
count=phone.count
count=count-1
if count==0:
#手机卖完了
print('%s 已售罄,请及时补货!'%phone.name)
self.phones_list.remove(phone)
else:
phone.count=count
return
else:
return 1
#更改产品信息
def update_phone(self):
print('1.添加新产品')
print('2.修改原有产品')
print('3.返回')
num=int(input('请选择您的操作:'))
while num not in range(1,4):
num=int(input('选择错误,请重选:'))
if num==1:
name=input('请输入添加的产品名称:')
price=input('请输入产品价格:')
#转换为数字
count=int(input('请输入添加的产品库存量(不能小于1):'))
while count<=0:
count=int(input('库存量不能小于0,请重输:'))
#声明一个对象
s=Phone(name,price,count)
#将小字典添加到大列表中
self.phones_list.append(s)
elif num==2:
if len(self.phones_list)<=0:
print('当前无商品信息!')
return
#输出手机详细信息
self.query_phone(1)
index=int(input('请输入您要修改的产品的序号:'))
while index not in range(0,len(self.phones_list)):
index=int(input('序号有误,请重选:'))
#根据index取出手机信息字典
phone=self.phones_list[index]
phone.name=input('请输入修改后的名称(%s):'%phone.name)
phone.price=input('请输入修改后的产品价钱(%s):'%phone.price)
count= int(input('请输入修改后的产品库存(%s)(不能小于1):' % phone.count))
#库存不能为0
while count<=0:
count=int(input('库存不能小于1,请重新输入:'))
phone.count=count
print('修改成功!')
else:
#结束函数执行
return
def del_phone(self):
if len(self.phones_list)<=0:
print('当前无产品信息!无法删除!')
return
print('1.查看所有产品,根据序号移除')
print('2.移除所有产品')
print('3.返回')
num = int(input('输入你的选择:'))
while num not in range(1, 4):
num = int(input('序号无效,请重输:'))
if num == 1:
for x in range(0, len(self.phones_list)):
print('产品序号:%s 产品名称:%s 价格:%s 库存:%s' % (x, self.phones_list[x].name, self.phones_list[x].price,self.phones_list[x].count))
num = int(input('输入您要删除的产品序号:'))
while num not in range(0, len( self.phones_list)):
num = int(input('序号无效,请重选:'))
rs = input('您确定要删除此产品吗?y/n:')
if rs == 'y':
del self.phones_list[num]
print('删除成功!')
else:
print('已取消删除操作!')
elif num == 2:
rs = input('您确定要删除所有产品吗?y/n:')
if rs == 'y':
self.phones_list.clear()
print('删除成功!')
else:
print('已取消删除操作!')
else:
print('返回上一级!')
def start(self):
while 1:
#ctrl+d 可以快速复制粘贴一行
print('1.查看所有手机品牌')
print('2.更改产品信息')
print('3.移除产品信息')
print('4.退出程序')
num=int(input('选择您的操作:'))
while num not in range(1,5):
num=int(input('选择错误,请重选:'))
if num==1:
self.query_phone(2)
self.buy_phone()
elif num==2:
self.update_phone()
elif num==3:
self.del_phone()
else:
break
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
phone=Phone_Syetem()
phone.start()
旅游查询:travel_class.py
import requests
import json
class Travel(object):
def __init__(self):
pass
def query(self):
while 1:
cityname=input('请输入您要查询的城市名称(输入0结束):')
if cityname=='0':
print('结束查询')
break
else:
url='http://api.map.baidu.com/telematics/v3/travel_city?location=%s&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json'%cityname
rs=requests.get(url)
rs_dict=json.loads(rs.text)
error=rs_dict['error']
if error==0:
results_list=rs_dict['result']
cityname=results_list['cityname']
print('*********欢迎查询%s的景点*********'%cityname)
itineraries_list=results_list['itineraries']
for detail_dict in itineraries_list:
name=detail_dict['name']
description=detail_dict['description']
print('%s: %s' % (name, description))
#表
itineraries=detail_dict['itineraries']
#遍历表,取出字典
count=1
for detail1_dict in itineraries:
print('*****************%s游之第%s天**********************'%(name,count))
description1=detail1_dict['description']
print('路径描述:%s' % description1)
dinning=detail1_dict['dinning']
print('就餐说明:%s'%dinning)
accommodation=detail1_dict['accommodation']
print('住宿说明:%s'%accommodation)
#path列表
path_list=detail1_dict['path']
#取出小字典
for path in path_list:
name1=path['name']
detail=path['detail']
print('景点名称:%s 访问地址:%s'%(name1,detail))
count+=1
print('--------------------------------------')
else:
print('查询失败!')
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
travel=Travel()
travel.query()
景点查询:scenery_spot_class.py
import requests
import json
class Scenery(object):
def __init__(self):
pass
def query(self):
while 1:
print('**************欢迎您使用景区查询系统*************')
scenery_name = input('请输入一个景区名字(输入英文字母)(按0退出):')
if scenery_name == '0':
print('成功退出景区查询系统,欢迎再次使用!')
break
else:
url = 'http://api.map.baidu.com/telematics/v3/travel_attractions?id=%s&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json' % scenery_name
rs = requests.get(url)
rs_dict = json.loads(rs.text)
error = rs_dict['error']
print(error)
if error == 0:
result_list = rs_dict['result']
name = result_list['name']
location = result_list['location']
lng = location['lng']
lat = location['lat']
telephone = result_list['telephone']
star = result_list['star']
print('**************%s简介**************' % name)
print('景点位置:经度:%s 纬度:%s' % (lng, lat))
print('联系电话:%s 星级:%s' % (telephone, star))
ticket_info = result_list['ticket_info']
price = ticket_info['price']
open_time = ticket_info['open_time']
attention = ticket_info['attention']
dict_1 = attention[0]
name_1 = dict_1['name']
description = dict_1['description']
print('价格:%s' % price)
print('开放时间:%s' % open_time)
print('%s:%s' % (name_1, description))
else:
print('查询失败!')
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
scenery=Scenery()
scenery.query()
内涵段子:NeiHanDuanZi_class.py
import requests
import json
class NeiHanDuanZi(object):
def __init__(self):
self.dict_1={1:'joke',2:'pic'}
def get(self):
while 1:
print('*******欢迎来到内涵社区*******')
print('1.段子')
print('2.图片')
print('0.退出')
num=int(input('请输入您感兴趣的话题序号:'))
while num not in range(0,3):
num=int(input('您的输入无效,请重输:'))
if num==0:
print('退出程序!')
break
sel_num=self.dict_1[num]
url='http://www.neihanshequ.com/%s/?is_json=1&app_name=neihanshequ_web&max_time=1516958864.81'%sel_num
rs=requests.get(url)
rs_dict=json.loads(rs.text)
msg=rs_dict['message']
if msg=='success':
file_handle=open('%s.txt'%sel_num,'w',encoding='utf-8')
data_dict=rs_dict['data']
tip=data_dict['tip']
# print('%s'%tip)
file_handle.write('%s\n'%tip)
data_list=data_dict['data']
for x in range(0,len(data_list)):
data=data_list[x]
group_dict=data['group']
text=group_dict['text']
# print('内涵段子 第%s条\n%s'%(x+1,text))
file_handle.write('内涵段子 第%s条:%s\n'%(x+1,text))
print('内涵段子数据爬取成功!')
file_handle.close()
else:
print('获取数据失败!')
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
nhdz=NeiHanDuanZi()
nhdz.get()
手机归属地:Mobile_home_class.py
import requests
import json
class Mobile_home(object):
def __init__(self):
pass
def query(self):
while 1:
phone=input('请输入您要查询的的手机号(输入0退出):')
if phone=='0':
print('退出程序!')
break
url='http://apis.juhe.cn/mobile/get?phone=%s&key=89baea64806cf2020fed945e44a65dd2'%phone
rs=requests.get(url)
rs_list=json.loads(rs.text)
error_code=rs_list['error_code']
if error_code==0:
result_dict=rs_list['result']
province=result_dict['province']
city=result_dict['city']
areacode=result_dict['areacode']
zip=result_dict['zip']
company=result_dict['company']
print('省份:%s'%province)
print('城市:%s'%city)
print('区域码:%s'%areacode)
print('邮编:%s'%zip)
print('公司:%s'%company)
else:
print('查询无果!')
# __name__ 值为 __main__ 表示是从当前文件直接运行的
# __name__ 值为 当前文件名称 表示别的文件引用执行的
# 如果是从当前文件直接运行的,执行以下的代码
if __name__ == '__main__':
mobile_home=Mobile_home()
mobile_home.query()