python爬虫练习--爬取某城市历史气象数据(待优化)
来源:互联网 发布:中标数据查询360 编辑:程序博客网 时间:2024/05/22 13:45
# -*- coding=utf-8 -*-from __future__ import print_function import urllib.request from bs4 import BeautifulSoup strYear = '2013' strFile = 'zhengzhou' + strYear + '.csv' f = open(strFile, 'w') for month in range(1, 13): if(month < 10): strMonth = '0' + str(month) else: strMonth = str(month) strYearMonth = strYear + strMonth print("\nGetting data for month" + strYearMonth + "...", end='') url = "http://lishi.tianqi.com/beijing/"+strYearMonth+".html" page = urllib.request.urlopen(url) #创建BeautifulSoup对象 soup = BeautifulSoup(page, "html.parser") weatherSet = soup.find(attrs={"class":"tqtongji2"}) if(weatherSet == None): print("fail to get the page", end='') continue for line in weatherSet.contents: if(line.__class__.__name__ == 'NavigableString'): continue if(len(line.attrs) > 0): continue lis = line.findAll('li') strDate = lis[0].text highWeather = lis[1].text lowWeather = lis[2].text weather = lis[3].text windDirection = lis[4].text windPower = lis[5].text f.write(strDate +',' + lowWeather +',' + highWeather + ','+weather + ',' +windDirection + ',' + windPower +'\n') print("done", end='') f.close()
参考资料:http://cuiqingcai.com/1319.html
0 0
- python爬虫练习--爬取某城市历史气象数据(待优化)
- Python 气象数据分析
- python爬虫实例(待补充)
- Python爬虫(一)--城市公交网络站点数据的爬取
- 读取中国气象城市数据JSON数据添加到数据库(省级以及城市数据)
- 气象城市ID列表
- python--- 备份日志(待优化)
- 我的Python爬虫笔记(待整理)
- Python 气象数据分析 -- 《Python 数据分析实战》
- python小练习(待完善)
- python网页爬虫练习
- python爬虫第一次练习
- Python爬虫练习
- python爬虫练习1
- python爬虫练习1
- python爬虫练习2
- python爬虫练习3
- python简单爬虫练习
- Python中的isinstance函数
- 新路程------imx6 uart和串口(4)
- DB2数据库 SQL语句判断两个日期 是否属于同一个季度
- 在二叉查找树中插入节点
- 使用Nexus搭建Maven私服
- python爬虫练习--爬取某城市历史气象数据(待优化)
- 第二章算法
- 计算机控制系统中的抗干扰技术
- Socket通信
- bzoj1616
- Latex Error “Font txsy at 540 not found“
- java编程思想(一)
- ZLib 解压缩工具
- 一些常用的指令mininet & sflow & floodlight qos