python解析中国天气网的天气数据
时间:2023-01-20 18:48:39
使用方法:terminal中输入
python weather.py http://www.weather.com.cn/weather/101010100.shtml
北京6天的天气数据 json格式
#coding=utf-8
#weather.py
import urllib
import re
import simplejson
import sys
if len(sys.argv) != 2:
print 'please enter: python ' + sys.argv[0] + ' <url>'
exit(0)
url = sys.argv[1];
def readurlPageContent(url):#获取页面的所有数据 逐行读取
webpage = urllib.urlopen(url);
line = webpage.readline();
data = ''
while line:
data = data + line.strip(); #去除每行两边的的空格
line = webpage.readline();
return data
def getDatabody(data):
reg = re.compile(r'(<div class=\"weatherYubaoBox\">(?:(?!<div)(?!</div).)*</div>)');
matchs = reg.findall(data,re.I);
if len(matchs) > 0:
return matchs[0]
return None
def getSixDayWeather(data):
regs = re.compile(r'>((?:(?!<)(?!>)(?!var).)+)<');
datas = regs.findall(data)
if len(datas) > 12:
datas = datas[12:len(datas)-7];#提取其中的78项 即后面6天的天气数据 每天相关的信息有13个
return datas
return None
data = readurlPageContent(url);
match_data = getDatabody(data)
if match_data == None :
print 'get weather data fail'
exit(0)
weathers_data = getSixDayWeather(match_data)
if weathers_data == None:
print 'get six day info fail'
exit(0)
count = len(weathers_data);
groups_item_count = count / 6;
weathers = {};
groups = [];
start = 0
for item in weathers_data :
if (start % groups_item_count) == 0:
groups = [];
weathers[('day'+str(start/groups_item_count + 1))] = groups;
groups.append(item)
start = start + 1
print simplejson.dumps(weathers, encoding='UTF-8', ensure_ascii=False)
注:本文中使用了第三方JSON库simplejson,安装方法参考下面:
1.下载:http://pypi.python.org/pypi/simplejson/ ,文件为压缩文件
2.解压:可以直接右键:解压文件.例如:D:/simplejson
3.开始---运行---输入命令:cmd
4.进入到目录(如:D:/simplejson)
cd D:/simplejson
5.运行安装文件:setup.py install
标签:python,json,中国天气网
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决python3.x安装numpy成功但import出错的问题
2023-12-29 22:33:44
Python实现比较两个文件夹中代码变化的方法
2022-04-07 23:16:08
JavaScript控制flash操作 兼容IE FF[译]
2009-11-29 16:28:00
![](https://img.aspxhome.com/file/UploadPic/200911/29/flashjr-60s.jpg)
python实现由数组生成对称矩阵
2022-12-09 15:01:53
使用python+poco+夜神模拟器进行自动化测试实例
2022-12-19 09:09:29
![](https://img.aspxhome.com/file/2023/8/92178_0s.jpg)
Matplotlib 生成不同大小的subplots实例
2022-12-22 03:16:26
![](https://img.aspxhome.com/file/2023/0/134920_0s.jpg)
python opencv设置摄像头分辨率以及各个参数的方法
2021-03-05 00:23:34
oracle中的ID号实现自增长的方法
2024-01-13 13:27:26
基于js粘贴事件paste简单解析以及遇到的坑
2024-04-22 22:24:17
实用的PHP带公钥加密类分享(每次加密结果都不一样哦)
2024-05-11 10:06:15
![](https://img.aspxhome.com/file/2023/3/125783_0s.png)
SQL Server里书签查找的性能伤害
2024-01-13 08:19:05
![](https://img.aspxhome.com/file/2023/6/106246_0s.png)
Python面向对象三大特征 封装、继承、多态
2022-01-09 04:31:51
![](https://img.aspxhome.com/file/2023/1/92501_0s.png)
Python中的“_args”和“__kwargs”用法详解
2023-03-11 02:10:38
浅谈Python类中的self到底是干啥的
2022-10-24 15:18:41
pytorch获取vgg16-feature层输出的例子
2021-04-16 20:56:47
一起感受HTML5和CSS3的能量[译]
2009-09-04 16:29:00
利用Python脚本实现自动刷网课
2023-04-20 12:39:16
![](https://img.aspxhome.com/file/2023/1/68901_0s.jpg)
INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别
2012-06-06 19:38:28
python使用json.dumps输出中文问题
2023-11-17 22:04:46
![](https://img.aspxhome.com/file/2023/8/71428_0s.jpg)
MySQL主从复制之半同步semi-sync replication
2024-01-20 09:48:16
![](https://img.aspxhome.com/file/2023/8/97568_0s.png)