• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现从百度API获取天气的方法

Python实现从百度API获取天气的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 百度地图api,python 百度api,python 百度翻译api,python api中文文档,python api等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python实现从百度API获取天气的方法。分享给大家供大家参考。具体实现方法如下:

import os
import urllib.request
import urllib.parse
import json
class weather(object):
    # 获取城市代码的uri
    code_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="
    # 获取天气信息的uri
    weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
    # 主处理逻辑
    def mainHandle(self):
        print("输入你要查询的天气:")
        city_name = input()
        uri = self.code_uri + urllib.parse.quote(city_name)
        ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
        if ret['errNum'] != 0:
            print(ret['retMsg'])
            return False
        else:
            weather_uri = self.weather_uri + ret['retData']['cityCode']
            data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
            if data['errNum'] == 0:
                ret_data = data['retData']
                output = "城市名:" + city_name + "\r\n"
                output += "更新时间:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
                output += "天气:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
                output += "当前温度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
                print(output)
                return True
            else:
                print(data['errMsg'])
                return False
if __name__ == "__main__":
    weather = weather()
    weather.mainHandle()</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python实现从百度API获取天气的方法
  • Python使用百度API上传文件到百度网盘代码分享
  • python中使用百度音乐搜索的api下载指定歌曲的lrc歌词

相关文章

  • Python正则表达式匹配HTML页面编码
  • Python使用scrapy抓取网站sitemap信息的方法
  • 用Python写的图片蜘蛛人代码
  • 使用Python读写文本文件及编写简单的文本编辑器
  • python原始套接字编程示例分享
  • python数据清洗系列之字符串处理详解
  • Python爬虫:通过关键字爬取百度图片
  • Python正则表达式实现截取成对括号的方法
  • Python模块学习 datetime介绍
  • 使用Python内置的模块与函数进行不同进制的数的转换

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python 基础知识之字符串处理
    • Python中强大的命令行库click入门教程
    • Python的装饰器模式与面向切面编程详解
    • 在Python中关于中文编码问题的处理建议
    • python的迭代器与生成器实例详解
    • Python闭包的两个注意事项(推荐)
    • 部署Python的框架下的web app的详细教程
    • 在Django中同时使用多个配置文件的方法
    • python 捕获shell脚本的输出结果实例
    • scrapy自定义pipeline类实现将采集数据保存到mongodb的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有