一个用Python编写抓取天气预报的代码示例
Python代码抓取获取天气预报信息源码讲解。这是一个用Python编写抓取天气预报的代码示例,用python写天气查询软件程序很简单。这段代码可以获取当地的天气和、任意城市的天气预报,原理是根据url找到网站截取相应的数据展现。
Python抓取天气预报源码中,需要用到的模块使用方法文章推荐:
1、Python sys模块常用方法图文详解
2、Python string模块字符串函数方法操作教程
3、Python re正则表达式操作指南
4、Python urllib2模块post/get下载网络资源
#!/usr/bin/env python
# 解释器路径
from HTMLParser import HTMLParser
import sys,urllib2,string,re
#导入使用方法模块
class HtmlParser(HTMLParser):
#定义一个类来完成这个功能
def __init__(self):
self.data=''
self.readingdata=0
HTMLParser.__init__(self)
def handle_starttag(self,tag,attrs):
if tag == 'td':
self.readingdata=1
def handle_data(self,chars):
if self.readingdata:
self.data+=chars
def handle_endtag(self,tag):
if tag=='td':
self.readingdata=0
def cleanse(self):
self.data = re.sub('\s+',' ', self.data)
def getdata(self):
self.cleanse()
return self.data
# this url is a place where you want to know the weather forecast
url="http://www.weather.com.cn/html/weather/101210501.shtml"
req=urllib2.Request(url)
fd=urllib2.urlopen(req)
tp=HtmlParser()
tp.feed(fd.read())
weather=tp.getdata()
# when you are getting a weather after parsering
# this weather string have 7 days weather forecast
#www.iplaypy.com
# the following if for my awesome format
weather=weather.split()
tag=[weather.index(i) for i in weather if '\xe6\x97\xa5' in i]
first=weather[:tag[1]]
second=weather[tag[1]:tag[2]]
if second[1]!=second[7]:second[1]+=' --> '+second[7]
second[2]=second[9]+' --> '+second[3]
second[0]=second[0][:-6]
second=second[:3]
third=weather[tag[2]:tag[3]]
if third[1]!=third[7]:third[1]+=' --> '+third[7]
third[2]=third[9]+' --> '+third[3]
third[0]=third[0][:-6]
third=third[:3]
weather=[' Weather:']+first+['|']+second+['|']+third
for i in weather:print i,
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/crawler/2659.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 支付宝官方给力福利!每天可领1个红包,直接当
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
我要分享到:
上一篇:Python大学英语四六级考试CET查分实现方法
下一篇:没有了
下一篇:没有了
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python抓取网页图本脚本源码下载
- • 黑客工具_Python多线程爬虫抓取扫描器
- • Python爬虫自动获取whois信息的方法源码
- • 抓取网页所有url的简单Python爬虫源码
- • python爬虫正则表达式 一定要知道的by地瓜
- • 自动抓取163新闻的Python爬虫源码
- • Python大学英语四六级考试CET查分实现方法
- • 一个用Python编写抓取天气预报的代码示例
- • Python urllib2爬虫爬取图片下载代码分享
- • 斯巴达Python_搜索引擎爬虫抓取_超清视频
图文精华 RECOMMEND
-
黑客工具_Python多线程爬虫抓取扫
-
python爬虫正则表达式 一定要知道
-
自动抓取163新闻的Python爬虫源码
-
Python大学英语四六级考试CET查分实
-
斯巴达Python_搜索引擎爬虫抓取_超
-
斯巴达Python_搜索引擎爬虫抓取_超
热点文章 HOT
- 斯巴达Python_搜索引擎爬虫抓取_超清视频
- 斯巴达Python_搜索引擎爬虫抓取_超清视频
- 抓取网页所有url的简单Python爬虫源码
- 黑客工具_Python多线程爬虫抓取扫描器
- Python urllib2爬虫爬取图片下载代码分享
- 一个用Python编写抓取天气预报的代码示例
- Python抓取网页图本脚本源码下载
- python爬虫正则表达式 一定要知道的by地瓜