PIL
requests
chardet
psutil
1.PIL
PIL模块提供了操作图像的强大功能
画一个验证码图片:
import random
from PIL import Image, ImageFont, ImageDraw, ImageFilter
def getChar():
return chr(random.randint(65, 90))
def getColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
def getColor1():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
width = 60 * 4
height = 60
imgbg = Image.new('RGB', (width, height), (255, 255, 255))
# 字体和大小
fonttype = ImageFont.truetype('comic.ttf', 30)
# 创建画笔工具
draw = ImageDraw.Draw(imgbg)
for x in range(width):
for y in range(height):
draw.point((x, y), fill=getColor())
for t in range(4):
draw.text((60 * t + 10, 10), getChar(), font=fonttype, fill=getColor1())
# 加点效果增加识别难度
imgbg = imgbg.filter(ImageFilter.EMBOSS)
imgbg.save('co.jpg', 'jpeg')
Pillow官方文档:
https://pillow.readthedocs.org/
2.requests
2.1无参数get请求
import requests
r = requests.get('https://www.douban.com/')
print(r.status_code)
print(r.text)
2.2带参数的请求
d = {'keyfrom': 'Skykai521',
'key': '977124034',
'type': 'data',
'doctype': 'json',
'version': '1.1',
'q': 'hello'}
r = requests.get('http://fanyi.youdao.com/openapi.do', params=d)
print(r.encoding) # 自动检测编码
print(r.json()) # 直接输出json
---
utf-8
{'translation': ['你好'], 'basic': {'us-phonetic': 'həˈlo', 'phonetic': 'həˈləʊ', 'uk-phonetic': 'həˈləʊ', 'explains': ['n. 表示问候, 惊奇或唤起注意时的用语', 'int. 喂;哈罗', 'n. (Hello)人名;(法)埃洛']}, 'query': 'hello', 'errorCode': 0, 'web': [{'value': ['你好', '您好', 'hello'], 'key': 'Hello'}, {'value': ['凯蒂猫', '昵称', '匿称'], 'key': 'Hello Kitty'}, {'value': ['哈乐哈乐', '乐扣乐扣'], 'key': 'Hello Bebe'}]}
2.3post请求
r = requests.post('https:www.baidu.com/', data={'key': 'value'})
参数也可以直接是json:
r = requests.post('https:www.baidu.com/', json={'key': 'value'})
上传文件:
r = requests.post('http://www.abc.com/', files={'file': open('co.jpg', 'rb')})
需要注意的是上传文件时要用rb模式读取文件,这样获取的bytes长度才会是文件的长度。
2.4设置headers、cookie
r = requests.get("https://www.baidu.com/", headers={'User-Agent': 'test', 'key': 'value'}, cookies={'key': 'value'})
2.5指定超时时间
r = requests.get("http://www.abc.com/", timeout=5) #超时时间为5秒
2.6获取cookies
r = requests.get("https://www.baidu.com/", headers={'User-Agent': 'test', 'key': 'value'}, cookies={'key1': 'value'})
print(r.cookies)
print(r.cookies['BIDUPSID'])
---
<RequestsCookieJar[<Cookie BIDUPSID=1A44C0F22C4F99D6C6D962A6AEFE2581 for .baidu.com/>, <Cookie PSTM=1511792881 for .baidu.com/>, <Cookie BD_NOT_HTTPS=1 for www.baidu.com/>]>
1A44C0F22C4F99D6C6D962A6AEFE2581
3.chardet
主要用来检测编码,不一定完全准确
import chardet
print(chardet.detect(b'abc'))
print(chardet.detect('中国中国中国'.encode('gbk')))
print(chardet.detect('の主要ニュース'.encode('euc-jp')))
---
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}
{'encoding': 'EUC-JP', 'confidence': 0.99, 'language': 'Japanese'}
confidence是准确度
4.psutil
import psutil
print(psutil.cpu_count()) # CPU逻辑数量
print(psutil.cpu_count(logical=False)) # CPU物理核心
print(psutil.cpu_times()) # CPU 的用户、系统、空闲时间
print(psutil.virtual_memory()) # 物理内存信息
print(psutil.disk_partitions()) # 磁盘信息
print(psutil.disk_usage('/')) # 路径所在磁盘内存使用情况
---
4
2
scputimes(user=15929.0908203125, system=3789.453125, idle=41347.59765625, interrupt=225.0002498626709, dpc=201.25689370930195)
svmem(total=4196450304, available=901439488, percent=78.5, used=3295010816, free=901439488)
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='', opts='cdrom'), sdiskpart(device='J:\\', mountpoint='J:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='K:\\', mountpoint='K:\\', fstype='', opts='removable')]
sdiskusage(total=66343333888, used=36701618176, free=29641715712, percent=55.3)