python爬虫练习--爬取某城市历史气象数据(待优化)
2017-05-11 17:11
543 查看
# -*- 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
相关文章推荐
- Python3.6爬虫练习之爬取全国大学省份数据
- Python爬虫练习之一:抓取美团数据
- Python爬虫实践:获取空气质量历史数据
- Python 爬虫 大量数据清洗 ---- sql语句优化
- Python爬虫实现数据可视化,为你做一个城市旅游数据分析
- Python 网络爬虫5 ---- 第一次实现抓取数据并且存放到mysql数据库中
- 《网络爬虫-Python和数据分析》数据库建库建表问题
- python 实现斐波那契数列,动态删除历史数据demo,很久以前写的,分享给大家 推荐
- #小练习 使用字典保存HTMLParser解析的数据 分类: python 小练习 HTMLParser 2013-11-08 20:51 574人阅读 评论(0) 收藏
- 采用python的pyquery引擎做网页爬虫,进行数据分析
- Python的网页爬虫&文本处理&科学计&机器学习&数据挖掘工具集
- python获取城市天气数据案例
- python 历史数据删除法则
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器库
- Python+Mysql生成zabbix统计数据(优化) 推荐
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- 读取中国气象城市数据JSON数据添加到数据库(省级以及城市数据)
- python 爬虫scrapy框架练习
- 利用Python中的matplotlib模块抓取yahoo finance里的历史数据并绘图
- #小练习 使用正则抓取oschina博客专区首页数据 分类: python 小练习 正则表达式 2013-11-11 17:22 604人阅读 评论(0) 收藏