python爬虫(12)获取七天内的天气
2017-03-15 20:23
309 查看
通过python脚本,来获取最近的天气情况
这样就不必每次打开网站查询天气了
思路分析:
1.决定好要抓取哪个城市的天气情况,获取天气预报网站的页面内容
2.匹配网页内容中天气情况
这样就不必每次打开网站查询天气了
思路分析:
1.决定好要抓取哪个城市的天气情况,获取天气预报网站的页面内容
2.匹配网页内容中天气情况
#!/usr/bin/env python # coding:utf-8 import urllib2,re from bs4 import BeautifulSoup import sys reload(sys) sys.setdefaultencoding('utf-8') #导入使用方法模块 #这是北京市天气地址,可以换成别的市区 url="http://www.weather.com.cn/weather/101010100.shtml" def getHtml(url): try: print url html = urllib2.urlopen(url).read()#.decode('utf-8')#解码为utf-8 except: return return html def getWeatherReport(html): if not html: print 'nothing can be found' return soup=BeautifulSoup(html,'lxml') try: items=soup.find("ul",{"class":"t clearfix"}) result = re.sub(re.compile('\n+'),"\n",str(items.text)) print result except: print "something was wrong" return return result html=getHtml(url) getWeatherReport(html)
相关文章推荐
- python 学习笔记 12 -- 写一个脚本获取城市天气信息
- python 学习笔记 12 -- 写一个脚本获取城市天气信息
- <Python 2.7>爬虫获取天气历史按月份存为txt文件
- python爬虫获取全国天气信息
- Python爬虫框架Scrapy实战之定向批量获取职位招聘信息
- python3获取天气信息
- Python天气预报采集器实现代码(网页爬虫)
- 【用Python写爬虫】获取html的方法【二】:使用pycurl
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- Python爬虫框架Scrapy实战教程---定向批量获取职位招聘信息
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- python获取天气信息写入原有的excel文档
- Python实战-天气预报项目-获取天气信息
- Python天气预报采集器实现代码(网页爬虫)
- Python 爬虫获取百度贴吧图片
- Python简单两步实现天气爬虫采集器
- Python3.x学习笔记[1]:2种简单爬虫获取京东价格
- python获取本地位置和天气
- Python网络编程 获取页面中的天气信息
- python 自动获取天气程序(V1)