Python爬取天气网历史天气数据
来源:互联网 发布:地图龟淘宝哪家的好 编辑:程序博客网 时间:2024/05/17 13:42
我的第一篇博客,哈哈哈,记录一下我的Python进阶之路!
今天写了一个简单的爬虫。
使用Python的requests 和BeautifulSoup模块,Python 2.7.12可在命令行中直接使用pip进行模块安装。爬虫的核心是利用BeautifulSoup的select语句获取需要的信息。
pip install requestspip install bs4
以武汉市2017年5~7月的历史为例爬取天气网中武汉市的历史天气数据。
7月对应的网址为http://lishi.tianqi.com/wuhan/201707.html
1.requests模块获取网页内容
url='http://lishi.tianqi.com/wuhan/201707.html'response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser')
2.利用.select语句找到网页中天气数据所在的div
weather_list = soup.select('div[class="tqtongji2"]')
3.找出日期、最高气温、最低气温、天气等数据,用li.string获取li中的信息。
ul_list = weather.select('ul')for ul in ul_list: li_list= ul.select('li') for li in li_list: li.string.encode('utf-8') #具体的天气信息
具体代码实现如下:
#encoding:utf-8 import requests from bs4 import BeautifulSoup urls = ["http://lishi.tianqi.com/wuhan/201707.html", "http://lishi.tianqi.com/wuhan/201706.html", "http://lishi.tianqi.com/wuhan/201705.html"] file = open('wuhan_weather.csv','w') for url in urls: response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') weather_list = soup.select('div[class="tqtongji2"]') for weather in weather_list: weather_date = weather.select('a')[0].string.encode('utf-8') ul_list = weather.select('ul') i=0 for ul in ul_list: li_list= ul.select('li') str="" for li in li_list: str += li.string.encode('utf-8')+',' if i!=0: file.write(str+'\n') i+=1 file.close()
最后的结果:
相比正则表达式,使用select语句爬取数据就是这么简单啦!
正则表达式还不是很理解,等理清楚,再来写总结。
阅读全文
1 0
- Python爬取天气网历史天气数据
- python爬取历史天气数据
- Python爬取中国天气网天气数据
- python爬取天气数据
- python爬取天气网历史数据
- Python 抓取中国天气网天气数据
- Python爬取天气信息
- Python-爬取中国天气网天气并通过邮箱定时发送
- python历史天气采集分析
- python多进程爬取天气信息
- Python3.x 爬虫 爬取中国天气网数据
- python3网络爬虫爬取天气网空气质量数据
- 爬取天气数据+热力图
- python爬天气网历史数据
- 中国天气网城市代码爬取
- 获取历史天气数据,数据挖掘必备
- 从网上抓取历史天气数据
- 简明 python例程 (爬取 某地一年天气最高温度)
- 动态方式破解apk前奏篇学习笔记
- 常用排序算法总结
- 基于webrtc的apprtc服务器搭建(ubuntu14.04 64位)
- java的练习
- torch.*Tensor expected, got userdata
- Python爬取天气网历史天气数据
- How to Install VIM without ROOT Permission
- 域名。端口映射配置
- 数据的统计特性
- Extjs6.2 Form上传图片之前预览
- mysql新建用户在本地无法登录
- unity3d 一种实体动画序列帧的效果
- 利用sklearn进行tfidf计算
- 虚拟机垃圾回收相关知识整理(JVM GC)