Python 爬取yahoo天气

一.使用urllib和xml 这两个Python内建库实现

from xml.parsers.expat import ParserCreate
from urllib import request

def fetch_xmldata(url):
    req = request.Request(url)
    req.add_header('User-Agent',
                   'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) \
                   Version/8.0 Mobile/10A5376e Safari/8536.25')
    with request.urlopen(req) as f:
        print('Status:', f.status, f.reason)
        return f.read().decode()

class DefaultSaxHandler(object):
    def __init__(self):
        self.city = ''
        self.forecast = []

    def start_element(self, name, attrs):
        #print('sax:start_element: %s, atts: %s' % (name, str(attrs)))
        if name == 'yweather:location':
            self.city = attrs['city']
        if name == 'yweather:forecast':
            self.forecast.append({'date':attrs['date'],
                                  'text':attrs['text'],
                                  'high':round((int(attrs['high']) - 32) / 1.8),
                                  'low' :round((int(attrs['low']) - 32) / 1.8)})

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

    def char_data(self, text):
        #print('sax:char_data: %s' % text)
        pass

def handler_xmldata(xmldata):
    handler = DefaultSaxHandler()
    parser = ParserCreate()
    parser.StartElementHandler = handler.start_element
    parser.EndElementHandler = handler.end_element
    parser.CharacterDataHandler = handler.char_data
    parser.Parse(xmldata)
    return {'city':handler.city, "forecast":handler.forecast}
if __name__ == '__main__':
    '''
    BeiJing weather A week
    '''
    URL = r'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather' \
          r'.forecast%20where%20woeid%20%3D%202151330&format=xml'
    xml = fetch_xmldata(URL)
    result= handler_xmldata(xml.encode('utf-8'))
    print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值