AZ0NE

关于我

网络安全爱好者

调用了百度天气预报API

 

#coding=utf-8
import urllib
from xml.parsers.expat import ParserCreate
import sys
class ParserXML(object):
    
    def start_element(self, name, attrs):
        print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))

    def end_element(self, name):
        print('sax:end_element: %s' % name)

    def char_data(self, text):
        print('sax:char_data: %s' % text)
city = raw_input()
city = city.decode(sys.stdin.encoding).encode('utf-8')
part1 = "http://api.map.baidu.com/telematics/v3/weather?location="
part2 = "&output=xml&ak=A95b994530a76ed90228f0cdbe1ef540"
url = part1+city+part2
print url
#xml = urllib.urlopen('http://api.map.baidu.com/telematics/v3/weather?location=哈尔滨&output=xml&ak=A95b994530a76ed90228f0cdbe1ef540').read()
xml = urllib.urlopen(url).read()
handler = ParserXML()
parser = ParserCreate()
parser.returns_unicode = True
parser.StartElementHandler = handler.start_element
parser.EndElementHandler = handler.end_element
parser.CharacterDataHandler = handler.char_data

parser.Parse(xml)

评论
热度(1)
© AZ0NE | Powered by LOFTER