一、常用内嵌模块
1、contextlib
(1)任何对象只要正确实现了上下文的管理,都可以用with结构。而上下文的管理是通过类的__enter__和__exit__两个方法实现的
class Find(object):
def __init__(self,name):
self.name= name
def __enter__(self):
print('Begin')
return self
def __exit__(self,exc_type,exc_value,traceback):
if(exc_type):
print('Error')
else:
print('End')
def find(self):
print('Find info about %s...’ % self.name)
调用时 with Find('Jamson') as f:
f.find()
(2)@contextmanager
编写__enter__和__exit__方法比较繁琐,可利用@contextmanager装饰器进行简化
from contextlib import contextmanager
class Find(object):
def __init__(self,name):
self.name= name
def find(self):
print('Find info about %s...’ % self.name)
@contextmanager
def creat_find(name):
print('Begin')
f = Find(name)
yield f
print('End')
with creat_find('Jamson') as f:
f.find()
2、urllib
(1)Get请求
from urllib import request
with urlopen('https://www.baid.com') as f:
data = f.read()
print('Status:', f.status,f.reason)
for k, v in f.getheads():
print('%s, %s' % (k, v))
print('Data:' data.encode('utf-8'))
(2)Post请求
from urllib import request,parse
email = 'JamsonWan@gmail.com'
password = 'Hello,world!'
login_data = parse.urlencode([
('username',email)
('password',password)
])
req = request.Request('http://192.168.2.230/login')
with urlopen(req, data = login_data.decode('utf-8') ) as f:
data = f.read()
.....
可以利用req.add_header('xxxx')#来模拟浏览器
3、XML
利用SAX解析器解析XML
3个方法start_element, char_data, end_element
4、HTMLParser
解析html中的内容
from htmlparser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print('<%s>' % tag)
.....
二、三方模块
1、requests
(1)需要先安装requests模块,如果安装了Anaconda可以直接使用
pip install requests
(2)Get请求
import requests
req = requests.get('www.baidu.com')
r.status_code
r.text
对于带参数的get请求,requests.get() 有一个params的命名参数,其为一个dict类型。还有一个headers命名参数,其类型也是dict
(3)Post请求
r = request.post('www.baidu.com',data={'account': 'Jamson@gmail.com', password: 'mypassword'})
传递json数据
params = {'account': 'Jamson@gmail.com', password: 'mypassword'}
r = requests.(url,json = params) #会自动序列化为json
(4)上传文件
upload_files = {'file': open('text.txt', 'rb')}
r = requests.post(url, files = upload_files)
2、chardet
用于检测字符的编码格式
import chardet
chardet.detect(b'hello, world')
data = '你好啊'.encode('gbk')
chardet.detect(data)
3、psutil
获取系统信息的模块
详情参照