python练习:分析xml,datetime

初学python,边用边学。先试用一下他的xml和datetime模块。

# coding:utf-8

import datetime as dt
import xml.dom as dom
import xml.dom.minidom as mn
from urllib import urlopen

doc
=urlopen("http://api.fanfou.com/statuses/user_timeline/某ID.rss")
str
=doc.read()
e
=mn.parseString(str)
dl
=dt.timedelta(hours=8)         #用这个来转换时区算了。
it=e.getElementsByTagName("item")  #getElementsByTagName可能要经常用到
for i in it:
    tt
=i.getElementsByTagName("title")[0].firstChild.data  #text node,cdata等都要用data获得
    dd=dt.datetime.strptime(i.getElementsByTagName("pubDate")[0].firstChild.data,
                            
"%a, %d %b %Y %H:%M:%S +0000")+dl  #时区不知道怎么才能识别出来,唯有用笨方法
    print dd,tt
raw_input()

 还是不太会用python的官方文档,里面很少提供例程,学习起来还是问题多多,还是google一下比较快捷。

posted on 2009-04-10 20:14  Q  阅读(498)  评论(0编辑  收藏  举报