类的应用1---景点、快递、内涵段子、天气预报(json数据的爬取)

景点

import requests
import json
class Attr(object):
    def __init__(self):
        self.name=''
    def attr(self):
        while 1:
            print('---------------景点查询系统-------------')
            self.name = input('请输入要查询的景点名称(字母)(输入0结束):')
            if self.name == '0':
                print('*********退出程序********')
                break
            else:
                url='http://api.map.baidu.com/telematics/v3/travel_attractions?id=%s&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json'%self.name
                rs=requests.get(url)
                rs_dict = json.loads(rs.text)
                error_code = rs_dict['error']
                if error_code == 0:
                    result = rs_dict['result']
                    # 根据索引取出城市天气信息字典
                    name= result['name']
                    location=result['location']
                    lat=location['lat']
                    lng=location['lng']
                    telephone=result['telephone']
                    star=result['star']
                    print('景点名字:%s\n景点坐标:经度:%s纬度:%s\n电话:%s\n星级:%s'%(name,lat,lng,telephone,star))
                    ticket_info=result['ticket_info']

                    price=ticket_info['price']
                    open_time=ticket_info['open_time']
                    print('景点收费:%s\n开放时间:%s\n'%(price,open_time))
                    attention=ticket_info['attention']
                    for msg in attention:
                        name=msg['name']
                        description=msg['description']
                        print('注意:\n名称:%s\n详细描述:%s'%(name,description))

                else:
                    print('没有查询到景点信息!')
if __name__=='__main__':

    att=Attr()
    att.attr()

快递

# 快递查询
# 引入requests包
# 从requests中引入get函数
import requests
from requests import get
# 引入json解析包
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('选择您的快递公司:'))
            if num == 0:
                break
            # 判断选项是否在范围
            while num not in range(1, 12):
                num = int(input('选项有误,请重选:'))
            # 根据选择的序号,作为key,取出对应的值
            type = self.kd_dict[num]
            postid = input('请输入您的快递单号:')
            # 拼接url地址
            url = 'http://www.kuaidi100.com/query?type=%s&postid=%s' % (type, postid)
            # 发送请求
            rs = requests.get(url)
            # 将拿回的json字符串转换为python中的字典
            kd_info = json.loads(rs.text)
            # 取出服务器返回的message消息,判断是否正常
            msg = kd_info['message']
            if msg == 'ok':
                print('您的快递%s物流信息如下:' % postid)
                # 取出快递物流信息列表
                data = kd_info['data']
                # for 循环遍历列表
                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

内涵段子

import requests
import json
class Nhdz(object):
    def __init__(self):
        pass
    def nhdz(self):
        url='http://www.neihanshequ.com/joke/?is_json=1&app_name=neihanshequ_web&max_time=1516958864.81'
        rs=requests.get(url)
        nhdz_info=json.loads(rs.text)
        # print(nhdz_info)
        msg=nhdz_info['message']
        file_handle=open('内涵段子.txt','w',encoding='utf-8')
        if msg=='success':
            data=nhdz_info['data']
            # print(data)
            has_more=data['has_more']
            # print(has_more)
            min_time=data['min_time']
            tip=data['tip']
            datas=data['data']
            # print('更多:%s\n最少时间:%s\n小建议:%s\n'%(has_more,min_time,tip))

            for data in datas:
                group=data['group']
                type=data['type']
                text=group['text']
                id=group['id']
                favorite_count=group['favorite_count']
                # print('类型:%s\n文本:%s\nid:%s\n点赞数:%s'%(type,text,id,favorite_count))

                file_handle.write('更多:%s\n'%has_more)
                file_handle.write('最少时间:%s\n'%min_time)
                file_handle.write('小建议:%s\n'%tip)
                file_handle.write('类型:%s\n'%type)
                file_handle.write('文本:%s\n'%text)
                file_handle.write('id:%s\n'%id)
                file_handle.write('点赞数:%s\n'%favorite_count)
        else:
            print(msg)

if __name__=='__main__':
    nh=Nhdz()
    nh.nhdz()

天气预报

# 天气预报
# 引入requests
import requests
# 引入python中内置的包 json,用来解析和生成json数据的
import json
class Weather(object):
    def __init__(self,city):
        self.city=city
    def query_weather(self):

        # url 统一资源定位符
        # windows  +  r  cmd  打开命令行工具  输入 pip install requests 回车
        url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak' \
              '=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%self.city

        # 使用requests发起请求,接受返回的结果
        rs = requests.get(url)
        # 使用loads函数,将json字符串转换为pyhton的字典或列表
        rs_dict = json.loads(rs.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']
            # 取出pm值
            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('没有查询到天气信息!')
    def start(self):
        self.query_weather()

if __name__=='__main__':
    while 1:
        # 输入查询的城市
        city = input('请输入要查询的城市名称(汉字):')
        if city=='0':
            break
        tq=Weather(city)
        tq.start()





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值