您的位置:首页 > 编程语言 > Python开发

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

2016-03-16 19:03 609 查看

利用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'))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: