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'))
相关文章推荐
- python Custom template tags and filters is not a valid tag library
- [python]我的第一只爬虫
- Python3.x爬虫教程:爬网页、爬图片、自己主动登录
- python实现监控服务器的UDP丢包情况
- Python操作MySQL数据库9个实用实例
- 基于协同过滤的推荐引擎
- 【python】scrapy初探_抓取糗事百科hot帖子
- Python 高级编程笔记之编写一个包
- 《笨办法学python3》的学习笔记(0-4)节
- Python的with open file as 自我了解解释
- python库requests登录zhihu
- Python学习之内建模块(5):HTMLParser
- Python的模块引用和查找路径
- Python图片处理库Wand的简单使用
- python kill进程
- python学习笔记-多进程
- python 2.7 rsa 离线安装 和使用示例
- python中的int与str
- python中的错误处理
- Python解析JSON