【练习】json访问api(python3.5)

json数据接口

天气接口
http://api.map.baidu.com/telematics/v3/weather?location=郑州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?
快递查询
http://www.kuaidi100.com/query?type=快递公司代号&postid=快递单号
ps:快递公司编码:申通="shentong" EMS="ems" 顺丰="shunfeng" 圆通="yuantong" 中通="zhongtong" 韵达="yunda" 天天="tiantian" 汇通="huitongkuaidi" 全峰="quanfengkuaidi" 德邦="debangwuliu" 宅急送="zhaijisong"
百度百科接口:
http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=关键字&bk_length=600
多米音乐接口
http://v5.pc.duomi.com/search-ajaxsearch-searchall?kw=关键字&pi=页码&pz=每页音乐数
电影接口(location=%s)
http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=郑州市&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json
旅游接口(location=%s)
http://api.map.baidu.com/telematics/v3/travel_city?location=郑州&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json
景点接口(输入景点拼音,id=%s)
http://api.map.baidu.com/telematics/v3/travel_attractions?id=yiheyuan&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json
ip定位接口
http://api.map.baidu.com/location/ip?ak=KQvhEkxc6mFCAYHTblC3NGVmxzxIWk0E&coor=bd09ll
ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ

代码格式化

在线代码格式化

一.查询天气

# 天气预报
# 引入requests
import requests
# 引入python中内置的包 json,用来解析和生成json数据的
import json
# 输入查询的城市
city = input('请输入要查询的城市名称:')

# url 统一资源定位符
# windows  +  r  cmd  打开命令行工具  输入 pip install requests 回车
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%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('没有查询到天气信息!')

二.定位ip并查询电影信息保存到表格
import requests
import json
import xlwt
#获取ip地址
url_city='http://api.map.baidu.com/location/ip?ak=KQvhEkxc6mFCAYHTblC3NGVmxzxIWk0E&coor=bd09ll'

rs=requests.get(url_city)
rs_dict=json.loads(rs.text)
#获取当前城市
content_dict=rs_dict['content']
address_detail=content_dict['address_detail']
city=address_detail['city']
#获取电影信息
url_movie='http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=%s&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&output=json'%city
rs=requests.get(url_movie)
rs_dict=json.loads(rs.text)
print('正在将%s电影信息写入表格'%city)
result_dict=rs_dict['result']
movie=result_dict['movie']
#写入excel中
count=1
workbook=xlwt.Workbook(encoding='utf-8')
sheet=workbook.add_sheet(u'电影信息表')
sheet.write(0,0,'电影名称')
sheet.write(0,1,'电影类型')
sheet.write(0,2,'发行时间')
sheet.write(0,3,'国家')
sheet.write(0,4,'演员名单')
sheet.write(0,5,'导演')
sheet.write(0,6,'电影题材')

for movie_dict in movie:
    movie_name=movie_dict['movie_name']
    movie_type=movie_dict['movie_type']
    movie_release_date=movie_dict['movie_release_date']
    movie_nation=movie_dict['movie_nation']
    movie_starring=movie_dict['movie_starring']
    movie_director=movie_dict['movie_director']
    movie_tags=movie_dict['movie_tags']
    #写入txt文本中
    # file_hande=open('movie.txt',mode='a')
    # file_hande.writelines([movie_name,movie_type,movie_release_date,movie_nation,movie_starring,movie_director,movie_tags])
    # file_hande.close()
    sheet.write(count, 0, movie_name)
    sheet.write(count, 1, movie_type)
    sheet.write(count, 2, movie_release_date)
    sheet.write(count, 3, movie_nation)
    sheet.write(count, 4, movie_starring)
    sheet.write(count, 5, movie_director)
    sheet.write(count, 6, movie_tags)
    count+=1

workbook.save('电影信息表.xls')


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值