Python学习之常用内置模块:urlib

利用urlib提供的request模块可以访问相应的url并且获取数据(以bytes的形式).下面的代码是通过urlib访问雅虎天气网站然后获取相应的数据.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from urllib import request, parse
import re,json

from xml.parsers.expat import ParserCreate
class WeatherSaxHandler(object):
    def __init__(self):
        self._days = 0
        self._weather = {}
    def start_element(self,name,attrs):
        if('city' in attrs or 'country' in attrs):self._weather['city'],self._weather['country'] = attrs['city'],attrs['country']
        if(('day' in attrs) and self._days == 0):
            self._days += 1
            self._weather['today']  = {'text':attrs['text'],'low':int(attrs['low']),'high':int(attrs['high'])}
        elif(('day' in attrs) and self._days == 1):
            self._days += 1
            self._weather['tomorrow'] ={'text':attrs['text'],'low':int(attrs['low']),'high':int(attrs['high'])}
    def end_element(self,name):
        pass
    def char_data(self,text):
        pass
def parse_weather(xml):
    weatherparser = WeatherSaxHandler()
    parser = ParserCreate()
    parser.StartElementHandler = weatherparser.start_element
    parser.EndElementHandler = weatherparser.end_element
    parser.CharacterDataHandler = weatherparser.char_data
    parser.Parse(xml)
    return weatherparser._weather
def fetch_xml(url):
    with request.urlopen(url) as f:
        data = f.read()
    print('aaaaaaaaaaaaaaaaaaa')
    return parse_weather(data.decode('utf-8'))

print(fetch_xml('http://weather.yahooapis.com/forecastrss?u=c&w=2151330'))

还可以模仿iphone6来访问url:这个时候需要使用Request对象然后往里面加参数来模拟iphone6的http申请

from urllib import request
req = request.Request('http://www.douban.com/')
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)
    for k, v in f.getheaders():
        print('%s: %s' % (k, v))
    print('Data:', f.read().decode('utf-8'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值