Python小练习-采集天气预报
2013-03-18 13:49
381 查看
View Code
代码很简单没什么好说的,主要就是正则匹配和乱码问题。
抓下来的页面是utf-8的 但是由于findall返回的是列表 如果直接输出列表 列表中有字符串的话会有乱码。
试过先转unicode再转gb2312也不可以。i.decode(‘utf-8′).encode(‘gb2312′)
单个输出就没问题了。
运行截图
#!/usr/bin/env python #coding=utf-8 import urllib import re def gethtml(url): page = urllib.urlopen(url) html = page.read() page.close() return html def getweather(html): reg = '<a title=.*?>(.*?)</a>.*?<span>(.*?)</span>.*?<b>(.*?)</b>' weatherlist = re.compile(reg).findall(html) return weatherlist getml = gethtml('http://sd.weather.com.cn/index.shtml') list_all = getweather(getml) for i in list_all: print i[0],i[1],i[2] # for item in i: # print item
代码很简单没什么好说的,主要就是正则匹配和乱码问题。
抓下来的页面是utf-8的 但是由于findall返回的是列表 如果直接输出列表 列表中有字符串的话会有乱码。
试过先转unicode再转gb2312也不可以。i.decode(‘utf-8′).encode(‘gb2312′)
单个输出就没问题了。
运行截图
![](http://images.cnitblog.com/blog/413053/201303/18180757-89d25e1922684406890e537f026ca264.png)
相关文章推荐
- 采集练习(四) python 获得hao123导航图片分类下的美女图片
- 采集练习(六) python获得chrome扩展 微度新标签页 下的云壁纸
- python练习之员工信息采集程序接口与判断
- 采集练习(十二) python 采集之 xbmc 酷狗电台插件
- python练习--360搜索关键字采集
- [Python]GUI编程练习 -- 获取天气预报
- 【Python】GUI 练习1--利率计算器
- Python小练习 - 判断是否为“回联文”
- python -练习5
- Python输入输出练习,运算练习,turtle初步练习
- python练习// 使用 generator 生成杨辉三角
- Python 参数练习
- python-练习实现猜数字的循环
- python练习5
- Python网络数据采集2-wikipedia
- python基础练习--求岁数问题
- 笨方法学Python 习题 24: 更多练习
- Python爬虫实战(3):安居客房产经纪人信息采集
- python二级练习(1)
- 结尾非零数的奇偶性 分类: python 小练习 2013-12-02 18:04 371人阅读 评论(0) 收藏