目录
- 分词 - jieba
- 🎈 词云库 - wordcloud
- 🎈 可视化进度条 - tpdm
- 🎈 优美的表格 - PrettyTable
- 🎈 多进程 - multiprocessing
- 🎈 多线程 - threading
- 🎈 谷歌翻译 - googletrans
- 🎈 重复回调 - retrying
- 🎈 游戏开发 - pygame
- 🎈 绘图教程 - turtle
- 🎈 数据分析 - pandas
- 🎈 算法加密 - pycryto
- 🎈 操作 win 电脑 - pywin32
- 🎈 自动程序测试 - Selenium
- 🎈 音频播放 - mp3play
- 🎈 网页解析 - BeautifulSoup
- 🎈 日志处理 - logging
- 🎈 图像处理 - PIL
- 🎈 发送邮件 - yagmail
- 🎈 源码打包 - pyinstaller
分词 - jieba
- 优秀的中文分词库,依靠中文词库,利用词库确定汉子之间关联的概率,形成分词结果
| import jieba |
| word = '伟大的中华人民共和国' |
| jieba.cut(word) |
| jieba.lcut(word) |
🎈 词云库 - wordcloud
- 对数据中出现频率较高的
关键词
生成的一幅图像,予以视觉上的突出
| import jieba |
| import numpy as np |
| import PIL.Image as Image |
| from wordcloud import WordCloud |
| def run(word_path, picture_path): |
| with open(word_path, 'r') as f: |
| word = f.read() |
| cut_word = ' '.join(jieba.cut(word)) |
| color_mask = np.array(Image.open(picture_path)) |
| word_cloud = WordCloud( |
| |
| font_path='/System/Library/Fonts/PingFang.ttc', |
| |
| background_color='white', |
| |
| mask=color_mask, |
| |
| max_words=120, |
| |
| max_font_size=2000 |
| ).generate(cut_word) |
| word_cloud.to_file('word_cloud.jpg') |
| im = word_cloud.to_image() |
| im.show() |
🎈 可视化进度条 - tpdm
- 好看的进度条,不仅会让人一眼就知道任务的进度,还能够让自己的心情愉悦
| from time import sleep |
| from tqdm import tqdm |
| |
| |
| for i in tqdm(range(1, 500)): |
| |
| sleep(0.01) |
| sleep(0.5) |
🎈 优美的表格 - PrettyTable
| import prettytable as pt |
| |
| tb = pt.PrettyTable() |
| tb.field_names = ['name', 'age', 'height', 'weight'] |
| tb.add_row(['飞兔', 25, 174, 65]) |
| tb.add_row(['autofelix', 23, 164, 55]) |
| tb.add_row(['极客飞兔', 27, 184, 69.5]) |
| print(tb) |
| |
| |
| |
| |
| |
| |
| |
🎈 多进程 - multiprocessing
| from multiprocessing import Process |
| def func(s): |
| print(s) |
| if __name__ == '__main__': |
| process = [ |
| Process(target=func, args=('1', )) |
| Process(target=func, args=('2', )) |
| ] |
| [p.start() for p in process] |
| [p.join() for p in process] |
🎈 多线程 - threading
| import threading |
| def func(s): |
| print(s) |
| if __name__ == '__main__': |
| thread = [ |
| threading.Thread(target=func, args=('1', )) |
| threading.Thread(target=func, args=('2', )) |
| ] |
| [t.start() for t in thread] |
| [t.join() for t in thread] |
🎈 谷歌翻译 - googletrans
| from googletrans import Translator |
| translator = Translator() |
| |
| translator.translate('안녕하세요.') |
| |
| translator.translate('안녕하세요.', dest='ja') |
| |
| translator.translate('极客飞兔', src='zh-cn') |
| |
| t = ttranslator.detect('이 문장은 한글로 쓰여졌습니다.') |
| t.lang |
🎈 重复回调 - retrying
- 如果请求失败,我们需要再重新进行进行请求,防止请求异常导致数据缺失
| from retrying import retry |
| @retry(stop_max_attempt_number=5) |
| def say(): |
| try: |
| autofelix |
| except Exception as e: |
| |
| print(e) |
| raise |
| say() |
🎈 游戏开发 - pygame
- 实现 python 游戏的开发,可以开发各种大小型游戏
| import pygame, sys |
| from pygame.locals import * |
| |
| pygame.init() |
| |
| screen = pygame.display.set_mode((500,400), 0, 32) |
| |
| pygame.display.set_caption('用户事件监控') |
| |
| screen.fill((255, 255, 255)) |
| |
| while True: |
| |
| for event in pygame.event.get(): |
| |
| if event.type == QUIT: |
| |
| pygame.quit() |
| |
| sys.exit() |
| |
| if event.type == KEYDOWN: |
| if(event.key==K_UP or event.key==K_w): |
| print("上") |
| if(event.key==K_DOWN or event.key==K_s): |
| print("下") |
| if(event.key==K_LEFT or event.key==K_a): |
| print("左") |
| if(event.key==K_RIGHT or event.key==K_d): |
| print("右") |
| |
| if(event.key==K_ESCAPE): |
| |
| pygame.quit() |
| |
| sys.exit() |
| |
| if event.type ==MOUSEMOTION: |
| print(event.pos) |
| |
| if event.type ==MOUSEBUTTONDOWN: |
| print("鼠标按下:", event.pos) |
| |
| if event.type ==MOUSEBUTTONUP: |
| print("鼠标抬起:", event.pos) |
| |
| pygame.display.update() |
🎈 绘图教程 - turtle
| from turtle import * |
| colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange'] |
| for x in range(360): |
| pencolor(colors[x % 6]) |
| width(x / 100 + 1) |
| forward(x) |
| left(59) |
🎈 数据分析 - pandas
- 数据分析处理库,为解决数据分析任务而创建的,能够快速便捷地处理数据的函数和方法
| import pandas as pd |
| info = pd.read_csv("students.csv", encoding = "utf-8") |
| |
| info.describe() |
| |
| pin = info["pin"] |
| pin_isnull = pd.isnull(pin) |
| pin_isnull_list = info[pin_isnull] |
| len(pin_isnull_list) |
| |
| books = info["life_cycle_books"] |
| book_isnull = pd.isnull(books) |
| book_list_isnull = info["life_cycle_books"][book_isnull == False] |
| mean = sum(book_list_isnull) / len(book_list_isnull) |
| |
| na_info = info.dropna(axis = 1) |
| |
| na_info = info.dropna(axis = 0, subset = ["age", "name"]) |
🎈 算法加密 - pycryto
- pycryto 能实现大致 3 种类型的数据加密(单向加密、对称加密 和非对称加密),产生随机数,生成密钥对,数字签名
| from Crypto.Hash import SHA256 |
| hash = SHA256.new() |
| hash.update('Hello, World!') |
| |
| digest = hash.digest() |
| |
| hexdigest = hash.hexdigest() |
| print(digest, hexdigest) |
🎈 操作 win 电脑 - pywin32
- pywin32 包装了 Windows 系统的 Win32 API,能创建和使用 COM 对象和图形窗口界面
| import win32api |
| import win32con |
| hid = win32gui.WindowFromPoint((100, 100)) |
| |
| title = win32gui.GetWindowText(hid) |
| |
| class_name = win32gui.GetClassName(hid) |
| |
| point = (400, 500) |
| win32api.SetCursorPos(point) |
| win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) |
| win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) |
🎈 自动程序测试 - Selenium
- Selenium 是一个用于 Web 应用程序测试的工具。Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样
| from selenium import webdriver |
| from selenium.webdriver import ActionChains |
| from selenium.webdriver.common.by import By |
| from selenium.webdriver.support.wait import WebDriverWait |
| from selenium.webdriver.support import expected_conditions as EC |
| |
| driver = webdriver.Chrome() |
| |
| driver.maximize_window() |
| |
| driver.get('https://sso.toutiao.com') |
| |
| WebDriverWait(self.driver, 10).until( |
| EC.text_to_be_present_in_element((By.XPATH, '//*[@id="mobile-code-get"]/span'), u'发送') |
| ) |
| |
| action = ActionChains(self.driver) |
| |
| action.click_and_hold(self.driver.find_element_by_xpath('//*[@id="captcha_container"]')).perform() |
| |
| action.move_by_offset(xoffset=x, yoffset=0).perform() |
| |
| action.release().perform() |
🎈 音频播放 - mp3play
- 一款超级小型的音频操作库,可以实现播放音乐,按空格键实现暂停和播放的切换
| import mp3play |
| clip = mp3play.load('music.mp3') |
| clip.play() |
🎈 网页解析 - BeautifulSoup
| from bs4 import BeautifulSoup |
| soup = BeautifulSoup('<p class="name nickname user"><b>i am autofelix</b></p>', 'html.parser') |
| |
| print(soup.p) |
| |
| print(soup.p.b) |
| |
| print(soup.p.text) |
| |
| print(soup.p.attrs) |
| |
| print(type(soup.p)) |
| |
| print(soup.p['class']) |
| |
| soup.p['class']=['Web','Site'] |
| print(soup.p) |
🎈 日志处理 - logging
| import logging |
| logging.basicConfig(filename='logging.text', level=logging.DEBUG) |
| logging.debug('It is a debug') |
| logging.info('It is a info') |
| logging.warning('It is a warning') |
🎈 图像处理 - PIL
- 非常适合于图像归档以及图像的批处理任务。可以使用 PIL 创建缩略图,转换图像格式,打印图像等等
| from PIL import Image |
| im = Image.open("picture.jpg") |
| new_im = im.convert('L') |
| print(new_im.mode) |
| new_im.show() |
🎈 发送邮件 - yagmail
- 是一种非常简单用来实现自动发邮件功能的包,可以实现给单人或者多人同时发送邮件
| import yagmail |
| |
| yag = yagmail.SMTP( user='邮箱地址', password='登录密码', host='smtp.163.com') |
| |
| contents = ['邮件第一行内容', '邮件第二行内容', '邮件第三行内容'] |
| |
| yag.send(['目标邮箱地址1', '目标邮箱地址2', '目标邮箱地址3'], '邮件标题', contents, ['c://附件.pdf', 'c://picture.jpg']) |
🎈 源码打包 - pyinstaller
pyinstaller -F -w -p ./lib -i logo.ico main.py