python實(shí)現(xiàn)自主查詢實(shí)時(shí)天氣
本文實(shí)例為大家分享了python實(shí)現(xiàn)自主查詢實(shí)時(shí)天氣的具體代碼,供大家參考,具體內(nèi)容如下
用到了urllib2 json 很簡單的一個(gè)應(yīng)用 如下

獲取城市編號
#coding=utf-8
import urllib2
url1 = 'http://m.weather.com.cn/data3/city.xml'
content1 = urllib2.urlopen(url1).read()
provinces = content1.split(',')
print content1 # 輸出content1可以查看全部省份代碼
result = ''
url = 'http://m.weather.com.cn/data3/city%s.xml'
for p in provinces:
p_code = p.split('|')[0]
url2 = url % p_code
content2 = urllib2.urlopen(url2).read() # 輸出content2可以查看此省份下所有城市代碼
cities = content2.split(',')
print content2
for c in cities:
c_code = c.split('|')[0]
url3 = url % c_code
content3 = urllib2.urlopen(url3).read()
print content3 #content3是此城市下所有地區(qū)代碼
districts = content3.split(',')
for d in districts: # 對于每個(gè)地區(qū),我們把它的名字記錄下來,然后再發(fā)送一次請求,得到它的最終代碼:
d_pair = d.split('|')
d_code = d_pair[0] #
if 5 == len(d_code):
continue
temp=[d_code]
temp.insert(4,0)
d_code ="".join(temp)
name = d_pair[1] # 名字
url4 = url % d_code
content4 = urllib2.urlopen(url4).read()
print content4
code = content4.split('|')[1]
line = "%s:%s\n" % (name, code)
result += line
print name + ':' + code
f = file('./city', 'w')
f.write(result)
f.close()
findweather
# -*- coding: utf-8 -*-
import urllib2
import json
city = {}
f =file('city','r')
src = f.readlines()
for line in src:
line = line.split('\n')[0]
name = line.split(':')[0]
code = line.split(':')[1]
city[name] = code
cityname = raw_input('請輸入你要查詢的城市名稱:\n')
citycode = city.get(cityname)
print cityname
if citycode:
try:
url = ('http://www.weather.com.cn/data/cityinfo/%s.html' % citycode)
content = urllib2.urlopen(url).read()
data = json.loads(content)
result = data['weatherinfo']
str_temp = ('%s\n%s ~ %s') % (result['weather'],result['temp1'],result['temp2'])
print str_temp
except:
print '查詢失敗'
else:
print '沒有找到該城市'
運(yùn)行 findweather 即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python爬蟲實(shí)例扒取2345天氣預(yù)報(bào)
- python3爬取各類天氣信息
- Python爬蟲天氣預(yù)報(bào)實(shí)例詳解(小白入門)
- python定時(shí)利用QQ郵件發(fā)送天氣預(yù)報(bào)的實(shí)例
- python結(jié)合API實(shí)現(xiàn)即時(shí)天氣信息
- Python爬取國外天氣預(yù)報(bào)網(wǎng)站的方法
- Python實(shí)現(xiàn)從百度API獲取天氣的方法
- python解析中國天氣網(wǎng)的天氣數(shù)據(jù)
- python顯示天氣預(yù)報(bào)
- Python天氣預(yù)報(bào)采集器實(shí)現(xiàn)代碼(網(wǎng)頁爬蟲)
相關(guān)文章
Python中Playwright?與?pyunit?結(jié)合使用詳解
這篇文章主要介紹了Python中Playwright?與?pyunit?結(jié)合使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用
這篇文章主要介紹了python3使用迭代生成器實(shí)現(xiàn)減少內(nèi)存占用的相關(guān)資料,需要的朋友可以參考下2021-05-05
一文帶你玩轉(zhuǎn)python中的requests函數(shù)
在Python中,requests庫是用于發(fā)送HTTP請求的常用庫,因?yàn)樗峁┝撕啙嵰子玫慕涌?,本文將深入探討requests庫的使用方法,感興趣的可以學(xué)習(xí)下2023-08-08
python實(shí)現(xiàn)TCPclient的使用示例
python實(shí)現(xiàn)TCPclient是一件簡單的事情,只要通過socket這個(gè)模塊就可以實(shí)現(xiàn),本文主要介紹了python實(shí)現(xiàn)TCPclient的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
python實(shí)現(xiàn)對doc,txt,xls文檔的讀寫操作
這篇文章主要介紹了python實(shí)現(xiàn)對doc,txt,xls文檔的讀寫操作,正如標(biāo)題所見,文章包括三個(gè)部分python實(shí)現(xiàn)對doc文檔的讀取、python實(shí)現(xiàn)對txt文檔的讀取和python實(shí)現(xiàn)對xls表格的讀取,需要的朋友可以參考一下2022-04-04
三個(gè)python爬蟲項(xiàng)目實(shí)例代碼
這篇文章主要介紹了三個(gè)python爬蟲項(xiàng)目實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Python?pandas中apply函數(shù)簡介以及用法詳解
apply()函數(shù)是pandas里面所有函數(shù)中自由度最高的函數(shù), apply()函數(shù)的參數(shù)是一個(gè)函數(shù)指針,這里可以使用lambda表達(dá)式幫助簡化代碼,下面這篇文章主要給大家介紹了關(guān)于Python?pandas中apply函數(shù)簡介以及用法的相關(guān)資料,需要的朋友可以參考下2022-09-09
GitHub 熱門:Python 算法大全,Star 超過 2 萬
4 月 27 日,GitHub 趨勢榜第 3 位是一個(gè)用 Python 編碼實(shí)現(xiàn)的算法庫,Star 數(shù)早已達(dá)到 26000+2019-04-04

