python 爬取天气温度
2015-12-04 00:00
357 查看
#!/usr/bin/env python # encoding: utf-8 import re import requests #定义访问地址列表,因为地址没有通配匹配所以没办法写正则 url = [ 'http://chengdu.tianqi.com/', 'http://chengdu.tianqi.com/chongzhou/', 'http://chengdu.tianqi.com/dayi/', 'http://chengdu.tianqi.com/dujiangyan/', 'http://chengdu.tianqi.com/jintang/', 'http://chengdu.tianqi.com/longquanyi/', 'http://chengdu.tianqi.com/pixian/', 'http://chengdu.tianqi.com/pujiang1/', 'http://chengdu.tianqi.com/pengzhou/', 'http://chengdu.tianqi.com/qionglai/', 'http://chengdu.tianqi.com/shuangliu/', 'http://chengdu.tianqi.com/wenjiang/', 'http://chengdu.tianqi.com/xindu/', 'http://chengdu.tianqi.com/xinjin/', ] #循环访问地址 for html in url: html=requests.get(html) detail = html.content.decode('gbk').encode('utf-8') detail1 = re.findall('<div id="rettemp">(.*?)</div>',detail,re.S) temper = re.findall('<strong>(.*?)</strong>',str(detail1),re.S) content = temper[0].replace('°','°') cityall = re.findall('<div class="temp">(.*?)<div class="weatherDiv1">',detail,re.S) city = [] for i in cityall: city = re.findall('<h3>(.*?)</h3>',i,re.S) for i in city: print i + ':' + content a = i + ':' + content + '\n' with open('temperature.txt','a+') as obj: obj.write(a) with open('temperature.txt','a+') as obj: obj.write('==================' + '\n') 说明: <div class="temp"> <h3>成都当前温度</h3> <div class="weatherDiv1"> <div class="numN"> 50-<br> 25-<br> 0-<br> -25-<br> -50- </div> <p class="bg_sk" style="height:37px" id="rethigh"></p> <div id="rettemp"><strong>9.6°</strong><span>相对湿度:99%</span></div> <div class="tq24_1"><a href="http://chengdu.tianqi.com/today/" target="_blank">24小时天气预报</a></div> </div> </div> detail1 = re.findall('<div id="rettemp">(.*?)</div>',detail,re.S) <div id="rettemp"><strong>9.6°</strong><span>相对湿度:99%</span></div> <div class="tq24_1"><a href="http://chengdu.tianqi.com/today/" target="_blank">24小时天气预报 7fe0 </a></div> </div> temper = re.findall('<strong>(.*?)</strong>',str(detail1),re.S) <strong>9.6°</strong> 运行结果: 成都当前温度:9.6° 崇州当前温度:9.9° 大邑当前温度:11.8° 都江堰当前温度:8.9° 金堂当前温度:9.9° 龙泉驿当前温度:9.6° 郫县当前温度:9.9° 蒲江当前温度:9.6° 彭州当前温度:9.7° 邛崃当前温度:11.7° 双流当前温度:10.2° 温江当前温度:10.8° 新都当前温度:9.8° 新津当前温度:10.5° 并会把结果存放到temperature.txt中
相关文章推荐
- python 爬取贴吧图片
- python操作excel的技巧整理
- python解析XML方法
- Conque ERROR: Python interface cannot be loaded
- Python.h: No such file or directory
- Python结构化编程
- python requests 高级用法 -- 包括SSL 证书错误的解决方案
- python execl转xml应用
- 使用Python开发windows GUI程序入门实例
- Python脚本自动化编译RPM包
- Python之路(二)
- 17.2. socket — Low-level networking interface Python
- python 中 类, 模块, 包的组织
- 趣学Python-教孩子学编程--第十二章
- 利用python中的pandas,sklearn进行数据挖掘 basic_of_datamining
- Python扩展库psutil用法精要
- Python学习笔记一
- 《python爬虫》学习笔记:urllib2库的使用
- Spark编程指南(Python版)
- LeetCode解题报告--Next Permutation