Python study notes --- commonly used third-party modules [Liao Xuefeng]

Common third-party modules

Pillow

PIL : Python Imaging Library, already the de facto image processing standard library for the Python platform . PIL is very powerful, but the API is very simple and easy to use.

Since PIL only supports Python 2.7 and has been in disrepair for a long time, a group of volunteers created a compatible version based on PIL , named Pillow , which supports the latest Python 3.x and added many new features. Therefore, we You can install and use Pillow directly.

manipulate image

Let's take a look at the most common image scaling operations , just three or four lines of code:

from PIL import Image

# 打开一个jpg图像文件,注意是当前路径:
im = Image.open('test.jpg')
# 获得图像尺寸:
w, h = im.size
print('Original image size: %sx%s' % (w, h))
# 缩放到50%:
im.thumbnail((w//2, h//2))
print('Resize image to: %sx%s' % (w//2, h//2))
# 把缩放后的图像用jpeg格式保存:
im.save('thumbnail.jpg', 'jpeg')

Other functions such as slice, rotate, filter, output text, palette, etc. are all available.

The blur effect is also just a few lines of code:

from PIL import Image, ImageFilter

# 打开一个jpg图像文件,注意是当前路径:
im = Image.open('test.jpg')
# 应用模糊滤镜:
im2 = im.filter(ImageFilter.BLUR)
im2.save('blur.jpg', 'jpeg')

PIL's == ImageDrawprovides a series of drawing methods ==, allowing us to draw directly. For example, to generate a letter verification code image:

from PIL import Image, ImageDraw, ImageFont, ImageFilter

import random

# 随机字母:
def rndChar():
    return chr(random.randint(65, 90))

# 随机颜色1:
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 随机颜色2:
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

# 240 x 60:
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
# font = ImageFont.truetype(r'C:\Windows\WinSxS\amd64_microsoft-windows-font-truetype-arial_31bf3856ad364e35_10.0.22000.1_none_c8c70dd5b224275c\Arial.ttf', 36)  #定位到字体文件的位置
font = ImageFont.truetype('arial.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill=rndColor())
# 输出文字:
for t in range(4):
    draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')

We fill the background with random colors, draw text, and finally blur the image to get the verification code picture as follows:

verification code

To learn more about the powerful functions of PIL, please refer to the official Pillow documentation:

https://pillow.readthedocs.org/

requests

We've covered Python's built-in urllib module for accessing network resources. However, it is cumbersome to use and lacks many useful advanced features. Use requests. It is a Python third-party library, which is particularly convenient for processing URL resources

from urllib import request

① To access a page via GET , only a few lines of code are needed:

>>> import requests
>>> r = requests.get('https://www.douban.com/') # 豆瓣首页
>>> r.status_code
200
>>> r.text
r.text
'<!DOCTYPE HTML>\n<html>\n<head>\n<meta name="description" content="提供图书、电影、音乐唱片的推荐、评论和...'

For URLs with parameters , pass in a dict as paramsparameter:

>>> r = requests.get('https://www.douban.com/search', params={
     
     'q': 'python', 'cat': '1001'})
>>> r.url # 实际请求的URL
'https://www.douban.com/search?q=python&cat=1001'

Coding problem

Requests automatically detects the encoding and can encodingbe viewed using attributes:

>>> r.encoding
'utf-8'

Whether the response is text or binary content, we can contentget bytesthe object with properties:

The webpage data obtained by the request is a byte, and the output needs to be format converted.encode("UTF-8")

>>> r.content
b'<!DOCTYPE html>\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n...'

③The convenience of requests is that for a specific type of response, such as JSON, you can directly obtain :

>>> r = requests.get('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%202151330&format=json')
>>> r.json()
{
    
    'query': {
    
    'count': 1, 'created': '2017-11-17T07:14:12Z', ...

Perform the .json() operation on the response object returned by the request initiation request ,.json operationWhat is returned is a dictionary type ( if the returned result is empty, an error will be reported )

④ When HTTP Header needs to be passed in , we pass in a dict as headersa parameter:

>>> r = requests.get('https://www.douban.com/', headers={
    
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit'})
>>> r.text
'<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n <title>豆瓣(手机版)</title>...'

⑤POST request

To send a POST request, simplyturn get()the method intopost(),Thenincoming dataparametersData as POST request:

>>> r = requests.post('https://accounts.douban.com/login', data={
    
    'form_email': '[email protected]', 'form_password': '123456'})

In the request in the urllib module, the difference between get and post is whether there is data in the data in the request.urlopen(params) params parameter .


⑥Coding form

requests uses application/x-www-form-urlencodedPOST data encoding by default. If you want to pass JSON data, you can directly pass in the json parameter :

params = {
    
    'key': 'value'}
r = requests.post(url, json=params) # 内部自动序列化为JSON

akin,Uploading files requires a more complex encoding format , but requests simplifies it into filesparameters

>>> upload_files = {
    
    'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=upload_files)

When reading the file, be carefulBe sure to 'rb'read in binary mode, so that the obtained byteslength is the length of the file.

Replace post()the method with put(), delete()etc., and you can request resources by PUT or DELETE.


Obtain other information about the HTTP response

In addition to easily obtaining the response content, requests is also very simple to obtain other information about the HTTP response. For example, to get response headers :

>>> r.headers
{
    
    Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Content-Encoding': 'gzip', ...}
>>> r.headers['Content-Type']
'text/html; charset=utf-8'

requests has special treatment for cookies, so that we can easily get the specified cookies without parsing cookies :

>>> r.cookies['ts']
'example_cookie_12345'

To pass Cookie in the request , just prepare a dict to pass in cookiesthe parameters:

>>> cs = {
    
    'token': '12345', 'status': 'working'}
>>> r = requests.get(url, cookies=cs)

Finally, to specify a timeout , pass in the timeout parameter in seconds:

>>> r = requests.get(url, timeout=2.5) # 2.5秒后超时

Guess you like

Origin blog.csdn.net/mwcxz/article/details/128713947