python結(jié)合API實(shí)現(xiàn)即時(shí)天氣信息
python結(jié)合API實(shí)現(xiàn)即時(shí)天氣信息
import urllib.request
import urllib.parse
import json
"""
利用“最美天氣”抓取即時(shí)天氣情況
http://www.zuimeitianqi.com/
"""
class ZuiMei():
def __init__(self):
self.url = 'http://www.zuimeitianqi.com/zuimei/queryWeather'
self.headers = {}
self.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36'
# 部分城市的id信息
self.cities = {}
self.cities['成都'] ='01012703'
self.cities['杭州'] = '01013401'
self.cities['深圳'] = '01010715'
self.cities['廣州'] = '01010704'
self.cities['上海'] = '01012601'
self.cities['北京'] = '01010101'
# Form Data
self.data = {}
self.city = '北京'
def query(self,city='北京'):
if city not in self.cities:
print('暫時(shí)不支持當(dāng)前城市')
return
self.city = city
data = urllib.parse.urlencode({'cityCode':self.cities[self.city]}).encode('utf-8')
req = urllib.request.Request(self.url,data,self.headers)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
# 解析json數(shù)據(jù)并打印結(jié)果
self.json_parse(html)
def json_parse(self,html):
target = json.loads(html)
high_temp = target['data'][0]['actual']['high']
low_temp = target['data'][0]['actual']['low']
current_temp = target['data'][0]['actual']['tmp']
today_wea = target['data'][0]['actual']['wea']
air_desc = target['data'][0]['actual']['desc']
# 上海 6~-2°C 現(xiàn)在溫度 1°C 濕度:53 空氣質(zhì)量不好,注意防霾。
print('%s: %s~%s°C 現(xiàn)在溫度 %s°C 濕度:%s %s'%(self.city,high_temp,low_temp,current_temp,today_wea,air_desc))
if __name__ == '__main__':
zuimei = ZuiMei()
zuimei.query('廣州')
效果演示:

相關(guān)文章
Python中用pyinstaller打包時(shí)的圖標(biāo)問題及解決方法
這篇文章主要介紹了python中用pyinstaller打包時(shí)的圖標(biāo)問題及解決方法,本文從兩方面給大家分析原因所在,通過截圖實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5信號(hào)與槽的連接
本文講解信號(hào)與槽的連接機(jī)制,詳細(xì)示范各種類型的信號(hào)/槽連接的實(shí)現(xiàn)方法,這是圖形用戶界面的核心內(nèi)容。還將介紹面向?qū)ο蟮某绦蛟O(shè)計(jì),這是圖形用戶界面的基本思想2021-10-10
python登錄WeChat 實(shí)現(xiàn)自動(dòng)回復(fù)實(shí)例詳解
在本篇內(nèi)容里小編給大家整理的是關(guān)于python登錄WeChat 實(shí)現(xiàn)自動(dòng)回復(fù)的相關(guān)實(shí)例內(nèi)容以及知識(shí)點(diǎn)總結(jié),有興趣的朋友們參考下。2019-05-05
python使用wmi模塊獲取windows下硬盤信息的方法
這篇文章主要介紹了python使用wmi模塊獲取windows下硬盤信息的方法,涉及Python獲取系統(tǒng)硬件信息的相關(guān)技巧,需要的朋友可以參考下2015-05-05
python3中@dataclass的實(shí)現(xiàn)示例
@dataclass?是 Python 3.7 引入的一個(gè)裝飾器,用于方便地定義符合數(shù)據(jù)類協(xié)議的類,本文主要介紹了python3中@dataclass的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-02-02
Python使用SQLAlchemy模塊實(shí)現(xiàn)操作數(shù)據(jù)庫
SQLAlchemy 是用Python編程語言開發(fā)的一個(gè)開源項(xiàng)目,它提供了SQL工具包和ORM對(duì)象關(guān)系映射工具,使用SQLAlchemy可以實(shí)現(xiàn)高效和高性能的數(shù)據(jù)庫訪問,下面我們就來學(xué)習(xí)一下SQLAlchemy模塊的具體應(yīng)用吧2023-11-11
基于OpenCv實(shí)現(xiàn)的人臉識(shí)別(附Python完整代碼)
人臉識(shí)別是基于人的臉部特征信息進(jìn)行身份識(shí)別的一種生物識(shí)別技術(shù),下面這篇文章主要給大家介紹了關(guān)于如何基于OpenCv實(shí)現(xiàn)的人臉識(shí)別,文中還附Python完整代碼,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Python開源自動(dòng)化工具Playwright安裝及介紹使用
playwright-python是一個(gè)強(qiáng)大的Python庫,僅用一個(gè)API即可自動(dòng)執(zhí)行Chromium、Firefox、WebKit等主流瀏覽器自動(dòng)化操作,本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2021-12-12

