python实现自主查询实时天气
作者:So_What1412 时间:2021-07-02 22:43:09
本文实例为大家分享了python实现自主查询实时天气的具体代码,供大家参考,具体内容如下
用到了urllib2 json 很简单的一个应用 如下
获取城市编号
#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是此城市下所有地区代码
districts = content3.split(',')
for d in districts: # 对于每个地区,我们把它的名字记录下来,然后再发送一次请求,得到它的最终代码:
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 '没有找到该城市'
运行 findweather 即可。
来源:https://blog.csdn.net/qq_31821675/article/details/78207893
标签:python,查询天气
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle DECODE函数语法使用介绍
2023-07-17 12:44:22
numpy的squeeze函数使用方法
2022-04-15 10:02:07
![](https://img.aspxhome.com/file/2023/3/75083_0s.png)
php 404错误页面实现代码
2023-11-15 07:58:31
关于Python 实现tuple和list的转换问题
2022-02-18 21:29:03
使用C#配合ArcGIS Engine进行地理信息系统开发
2023-06-25 16:35:01
![](https://img.aspxhome.com/file/2023/6/61966_0s.jpg)
Python接口自动化浅析数据驱动原理
2022-02-22 21:52:01
![](https://img.aspxhome.com/file/2023/8/103188_0s.png)
[译]JavaScript中的Timer是怎么工作的
2009-02-06 15:39:00
![](https://img.aspxhome.com/file/UploadPic/20092/6/timers-65s.png)
php获取当前时间的毫秒数的方法
2023-06-28 06:43:37
ORACLE 自动提交问题
2023-07-24 10:43:13
![](https://img.aspxhome.com/file/2023/5/63355_0s.jpg)
Python数据结构与算法之字典树实现方法示例
2022-02-28 19:42:37
python实现的web监控系统
2022-01-28 20:31:57
![](https://img.aspxhome.com/file/2023/4/91824_0s.jpg)
Python zip函数打包元素实例解析
2023-04-18 12:53:52
解决Python2.7中IDLE启动没有反应的问题
2022-10-17 17:43:57
![](https://img.aspxhome.com/file/2023/4/110044_0s.jpg)
25个值得收藏的Python文本处理案例
2022-06-19 15:38:29
Python如何利用%操作符格式化字符串详解
2022-07-17 14:08:39
![](https://img.aspxhome.com/file/2023/1/80381_0s.png)
server application error--IIS故障
2009-06-11 12:50:00
django 实现编写控制登录和访问权限控制的中间件方法
2021-04-26 21:08:53
python协程与 asyncio 库详情
2023-08-23 12:08:36
![](https://img.aspxhome.com/file/2023/4/62874_0s.webp)
记录Python脚本的运行日志的方法
2022-09-25 08:11:12
python openssl模块安装及用法
2023-07-03 12:01:04