Python工具函数

格式化输出当前时间

import datetime

now = datetime.datetime.now()
print(now)
print('login success:', now.strftime('%Y-%m-%d %H:%M:%S.%f'))

词云

import jieba
import wordcloud
from matplotlib import pyplot as plt

danmustr="需要制作词云的数据"
words = list(jieba.cut(danmustr))#分词
fnl_words = [word for word in words if len(word)>1]#去掉单字
wc = wordcloud.WordCloud(width=1000, height=800,font_path='Fonts/font1.ttf')
#设定词云画的大小字体,一定要设定字体,否则中文显示不出来
wc.generate(' '.join(fnl_words))

plt.imshow(wc)#看图
wc.to_file("danmu_pic.png")#保存

selenium使用代理

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://114.116.227.25:8118")
driver = webdriver.Chrome(
            executable_path='C:\Program Files (x86)\Google\Chrome\Application\chromedriver',
			options=options
)
url='https://www.baidu.com/s?wd=ip'
driver.get(url=url)

MD5SHA1

import hashlib

msg='杨python'
# 添加一些内容,提高加密复杂度
obj1=hashlib.md5('fsafaf'.encode('utf-8'))    # 'fsafaf' 为加密密钥
obj1.update(msg.encode('utf-8'))
result1=obj1.hexdigest()    
print(result1)

# sha1(40) sha256(64) sha384(96) sha512(128) md5(32)
obj2=hashlib.sha512('fsafaasfaf'.encode('utf-8'))
obj2.update(msg.encode('utf-8'))
result2=obj2.hexdigest()
print(result2)

# update 方法,可以分段保存明文,最后一起加密为密文
msg='杨pythoasfafan'.encode('utf-8')
md5 = hashlib.md5()
md5.update(msg[:6])
md5.update(msg[6:])
md5 = md5.hexdigest()
print(md5)

yamail邮件发送

import yagmail

# 链接邮箱服务器
yag = yagmail.SMTP(user='youremail', password='youpassword', host='smtp.qq.com')
#  新浪邮箱:smtp.sina.com,搜狐邮箱:smtp.sohu.com  'smtp.163.com',25
# 邮箱正文
contents = ['This is the body, and here is just text You can find an audio file attached.']
# 发送邮件
 html = """
<html>
   <body>
     <p>
        Here is the <a href="http://www.baidu.com">link</a> you wanted.
     </p>

   </body>
 </html>
 """
html=r'D:\my_projects\spider\py\工具模块\test.html'

# yag.send('yjs13689179415@163.com', 'subject', contents)
# 发送附件   ['aaa@qq.com','bbb@qq.com']   interable
# yag.send('yjs13689179415@163.com', subject='音乐', contents=html,attachments=["D:\python_study\Song\句号.mp3"])
yag.send('1307534519yjs@gmail.com', subject='answer', contents=html)

ffmpeg命令视频合成

import os
import sys
import subprocess

"""
windows下path中直接配置的变量无法使用,需要加全路径
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
ffmpeg -i "concat:1.ts|2.ts|3.ts|" -c copy output.mp4

filelist.txt 文件內容
file  '1.ts'
file  '2.ts'
file  '3.ts'
file  '4.ts'
"""
#切换目录
os.chdir("D:\my_projects\spider\py")

cmd=r'D:\ffmpeg\bin\ffmpeg -i "concat:1.ts|2.ts|3.ts|" -c copy output.mp4'
os.system(cmd)

PDF合成

import pdfkit


config=pdfkit.configuration(wkhtmltopdf=r'D:\PyCharm 2019.3.2\wkhtmltopdf\bin\wkhtmltopdf.exe')
options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "utf-8",
    'custom-header' : [
        ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None,
    'footer-right':'[1]'
}
css = 'test.css'
 test=
 <div>iafiahfiafh</div>

pdfkit.from_string('<div>'+str2+'</div>', 'out.pdf',configuration=config,css=css,options=options)
with open(r'/py/test\1.png', 'rb') as file:
       str=file.read()
pdfkit.from_url()
pdfkit.from_string(str.encode('utf-8'), 'out.pdf',configuration=config,options=options)

RSA加密

import base64
import os
from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA


# # Client的秘钥对的生成
# private_pem = rsa.exportKey()
# with open("client-private.pem", "w") as f:
#     f.write(private_pem)
#
# public_pem = rsa.publickey().exportKey()
# with open("client-public.pem", "w") as f:
#     f.write(public_pem)


class RsaCode():
    def __init__(self):
        # 伪随机数生成器
        self.random_generator = Random.new().read
        self.rsa = None

    def CreateServerKey(self):
        # rsa算法生成实例
        self.rsa = RSA.generate(1024, random_generator)
        private_pem = rsa.exportKey()
        with open("private.pem", "wb") as file:
            file.write(private_pem)
        public_pem = rsa.publickey().exportKey()
        with open("public.pem", "wb") as file:
            file.write(public_pem)

    def RsaEncrypt(self, content):
        while True:
            if os.path.exists('public.pem'):
                with open('public.pem', 'rb+') as f:
                    key = f.read()
                break
            else:
                self.CreateServerKey()
                break
        RsaKey = RSA.importKey(key)
        cipher = Cipher_pkcs1_v1_5.new(RsaKey)
        cipher_text = base64.b64encode(cipher.encrypt(content.encode()))
        return cipher_text.decode()

    def RsaDecrypt(self, content):
        if os.path.exists('private.pem'):
            with open('private.pem', 'rb+') as f:
                key = f.read()
            RsaKey = RSA.importKey(key)
            cipher = Cipher_pkcs1_v1_5.new(RsaKey)
            text = str(cipher.decrypt(base64.b64decode(content), self.random_generator), "utf-8")
            return text
        else:
            print('no private.pem please create!')


if __name__ == '__main__':
    mcode = RsaCode()
    msg='啥都不会司法不公i三个'
    res1 = mcode.RsaEncrypt(msg)
    print(res1)
    res2 = mcode.RsaDecrypt(res1)
    print(res2)

百度语音合成

"""
pip install baidu-aip

"""

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = 'xxxxx'
API_KEY = 'xxxxxx'
SECRET_KEY = 'xxxxxxx'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis('你好百度', 'zh', 4, {'vol': 5, })
if not isinstance(result, dict):
    with open('auido.mp3', 'wb') as f:
        f.write(result)
'''
tex	     String	合成的文本,使用UTF-8编码, 请注意文本长度必须小于1024字节	是
cuid	String	用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内	否
spd	    String	语速,取值0-9,默认为5中语速	否
pit	    String	音调,取值0-9,默认为5中语调	否
vol	    String	音量,取值0-15,默认为5中音量	否
per	    String	发音人选择, 0为女声,1为男声3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女	否
https://ai.baidu.com/ai-doc/SPEECH/1k4o0bmc7
'''

百度AI图像识别

from aip import AipOcr
import requests

APP_ID = 'xxxxx'

API_KEY = 'xxxxxxx'

SECRET_KEY = 'xxxxxxxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


def get_file_content(filePath):
     with open(filePath, 'rb') as fp:
        return fp.read()



image = get_file_content('/py/图片test/code2.png')

# 图片中获取文字验证码
 with open(img, 'rb') as fp:
        image=fp.read()
result2=client.basicAccurate(image)
print(result2)

机器人

# -*- coding: UTF-8 -*-
import hashlib
import urllib
import urllib.request
from urllib import parse
import base64
import time
import requests

url_preffix = 'https://api.ai.qq.com/fcgi-bin/'








class AiPlat(object):
    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.data = {}


    def setParams(self,array, key, value):
        array[key] = value

    def getOcrGeneralocr(self, image):
        self.url = url_preffix + 'ocr/ocr_generalocr'
        self.setParams(self.data, 'app_id', self.app_id)
        self.setParams(self.data, 'app_key', self.app_key)
        self.setParams(self.data, 'time_stamp', int(time.time()))
        self.setParams(self.data, 'nonce_str', int(time.time()))
        image_data = base64.b64encode(image)
        self.setParams(self.data, 'image', image_data)
        sign_str = self.genSignString(self.data)
        self.setParams(self.data, 'sign', sign_str)
        return requests.post(url=self.url, data=self.data).json()

    def getAaiWxAsrs(self, chunk, speech_id, end_flag, format_id, rate, bits, seq, chunk_len, cont_res):
        self.url = url_preffix + 'aai/aai_wxasrs'
        self.setParams(self.data, 'app_id', self.app_id)
        self.setParams(self.data, 'app_key', self.app_key)
        self.setParams(self.data, 'time_stamp', int(time.time()))
        self.setParams(self.data, 'nonce_str', int(time.time()))
        speech_chunk = base64.b64encode(chunk)
        self.setParams(self.data, 'speech_chunk', speech_chunk)
        self.setParams(self.data, 'speech_id', speech_id)
        self.setParams(self.data, 'end', end_flag)
        self.setParams(self.data, 'format', format_id)
        self.setParams(self.data, 'rate', rate)
        self.setParams(self.data, 'bits', bits)
        self.setParams(self.data, 'seq', seq)
        self.setParams(self.data, 'len', chunk_len)
        self.setParams(self.data, 'cont_res', cont_res)
        sign_str = self.genSignString(self.data)
        self.setParams(self.data, 'sign', sign_str)
        return requests.post(url=self.url, data=self.data).json()

    def getNlpTextChat(self, session, question):
        self.url = url_preffix + 'nlp/nlp_textchat'
        self.setParams(self.data, 'app_id', self.app_id)
        self.setParams(self.data, 'app_key', self.app_key)
        self.setParams(self.data, 'time_stamp', int(time.time()))
        self.setParams(self.data, 'nonce_str', int(time.time()))
        self.setParams(self.data, 'session', session)
        self.setParams(self.data, 'question', question)
        sign_str = self.genSignString(self.data)
        self.setParams(self.data, 'sign', sign_str)
        return requests.post(url=self.url, data=self.data).json()

    def genSignString(self,parser):
        uri_str = ''
        for key in sorted(parser.keys()):
            if key == 'app_key':
                continue
            uri_str += "%s=%s&" % (key, urllib.parse.quote(str(parser[key]), safe=''))
        sign_str = uri_str + 'app_key=' + parser['app_key']
        hash_md5 = hashlib.md5(sign_str.encode("latin1"))
        return hash_md5.hexdigest().upper()
import os
import webbrowser
import apiutil
from aip import AipSpeech
# import playsound


class MyRobot:
    def __init__(self):
        self.app_id = 'xxxx'
        self.app_key = 'xxxxx'
        self.al_api_key = 'xxxxx'
        self.al_secret_key = 'xxxxxx'
        self.token_url = 'http://openapi.baidu.com/oauth/2.0/token'

    def speak(self, text):
        session = 10000
        ai_object = apiutil.AiPlat(self.app_id, self.app_key)
        response = ai_object.getNlpTextChat(session, text)
        return response


if __name__ == '__main__':
    mrobot = MyRobot()
    while True:
        you_input = input('你: ')
        response = mrobot.speak(you_input)
        # if response['ret'] == 0:
        #     print('智障机器人:', response['data']['answer'])
        #     APP_ID = '18483905'
        #     API_KEY = 'Wh69scX5LABlqwTtB2huK1W7'
        #     SECRET_KEY = 'VZMyhaxgELic0my7o6xGIPvrEIa24FOX'
        #     client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
        #     result = client.synthesis(response['data']['answer'], 'zh', 4, {'vol': 5, })
        #     if not isinstance(result, dict):
        #         with open('test.mp3', 'wb') as f:
        #             f.write(result)
        #     playsound.playsound('test.mp3')
        #     os.remove('test.mp3')
        # else:
        #     response = mrobot.speak(you_input)
        #     print('智障机器人:', response['data']['answer'])
        # if "我想搜索" in you_input:
        #     ans = input('输入你要搜锁的内容:')
        #     ans_url = 'https://www.baidu.com/s?wd=' + ans
        #     webbrowser.get().open(ans_url)
        print(response)

定时器

from threading import Timer

'''
https://blog.csdn.net/brucewong0516/article/details/84589616
func中的参数以元组的形式传入
eg:
    (1,)
'''

def func():
    print('test')
    global timer
    timer = Timer(2, func)
    timer.start()


timer = Timer(2, func)
timer.start()
# timer.cancel()  关闭计时器

Google 翻译

from googletrans import Translator

'''
ZH,    // 中文       "zh-CN"
EN,    // 英语       "en"
JP,    // 日语       "ja"
JPKA,  // 日语假名
TH,    // 泰语
FRA,   // 法语       "fr"
SPA,   // 西班牙语   "es"   <---  添加语种(自定义语种代号)
KOR,   // 韩语       "ko"
支持的语言到LANGUAGES里
    'af':'南非荷兰语',
     'sq':'阿尔巴尼亚语',
     'am':'阿姆哈拉语',
     'ar':'阿拉伯语',
     'hy':'亚美尼亚语',
     'az':'阿塞拜疆语',
     'eu':'巴斯克',
    'be':'白俄罗斯语',
    'bn':'孟加拉',
    'bs':'波斯尼亚语',
    'bg':'保加利亚语',
    'ca':'加泰罗尼亚语',
    'ceb':'宿务语',
    'ny':'奇切瓦',
    'zh-cn':'中文(简体)',
    'zh-tw':'中文(繁体)',
    'co':'科西嘉',
    'hr':'克罗地亚语',
    'cs':'捷克',
    'da':'丹麦语',
    'nl':'荷兰语',
    'en':'english',
    'eo':'世界语',
    'et':'estonian',
    'tl':'菲律宾',
    'fi':'芬兰语',
    'fr':'法语',
    'fy':'frisian',
    'gl':'加利西亚语',
    'ka':'乔治亚风格',
    'de':'德语',
    'el':'希腊语',
    'gu':'gujarati',
    'ht':'haitian creole',
    'ha':'hausa',
    'haw':'hawaiian',
    'iw':'希伯来语',
    'hi':'hindi',
    'hmn':'hmong',
    'hu':'匈牙利语',
    'is':'icelandic',
    'ig':'igbo',
    'id':'印度尼西亚语',
    'ga':'irish',
    'it':'意大利语',
    'ja':'日语',
    'jw':'javanese',
    'kn':'kannada',
    'kk':'kazakh',
    'km':'khmer',
    'ko':'韩文',
    'ku':'kurdish(kurmanji)',
    'ky':'kyrgyz',
    'lo':'老挝',
    'la':'拉丁',
    'lv':'拉脱维亚人',
    'lt':'立陶宛语',
    'lb':'卢森堡',
    'mk':'马其顿语',
    'mg':'马达加斯加',
    'ms':'马来语',
    'ml':'马拉雅拉姆语',
    'mt':'马耳他语',
    'mi':'毛利人',
    'mr':'marathi',
    'mn':'蒙古语',
    'my':'缅甸(缅甸)',
    'ne':'nepali',
    '否':'挪威语',
    'ps':'pashto',
    'fa':'波斯人',
    'pl':'抛光',
    'pt':'葡萄牙语',
    'pa':'旁遮普语',
    'ro':'罗马尼亚语',
    'ru':'russian',
    'sm':'萨摩亚语',
    'gd':'苏格兰盖尔语',
    'sr':'塞尔维亚语',
    'st':'sesotho',
    'sn':'shona',
    'sd':'sindhi',
    'si':'僧伽罗语',
    'sk':'斯洛伐克',
    'sl':'斯洛文尼亚语',
    'so':'索马里',
    'es':'西班牙语',
    'su':'sundanese',
    'sw':'swahili',
    'sv':'瑞典语',
    'tg':'塔吉克',
    'ta':'泰米尔语',
    'te':'telugu',
    'th':'thai',
    'tr':'土耳其语',
    'uk':'乌克兰语',
    'ur':'urdu',
    'uz':'uzbek',
    'vi':'越南',
    'cy':'威尔士语',
    'xh':'xhosa',
    'yi':'yiddish',
    'yo':'yoruba',
    'zu':'zulu',
    'fil':'菲律宾',
    'he':'希伯来语'
'''

# 设置Google翻译服务地址
translator = Translator(service_urls=[
      'translate.google.cn'
    ])
# translator = Translator()
print(translator.translate('美しい天気',dest='en', src='auto').text)
print(translator.translate('hello', dest='zh-CN').text)
print(translator.translate('今天天气不错', dest='ar').text)
print(translator.translate('今天天气不错', dest='ko').text)
print(translator.translate('今天天气不错', dest='es').text)
print(translator.translate('今天天气不错', dest='fr').text)

阿里云短信发送

pip install aliyun-python-sdk-core
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json
client = AcsClient('xxxxxx', 'xxxxxxxx', 'cn-hangzhou')

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')

request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', "xxxxxx")
request.add_query_param('SignName', "xxxxx")
request.add_query_param('TemplateCode', "SMS_xxxx")
request.add_query_param('TemplateParam', json.dumps({'code':'hello'}))

response = client.do_action(request)

print(str(response, encoding = 'utf-8'))

获取随机UA

    def get_ua():
        uas = []
        uafile = r'UAS.txt'
        with open(uafile, 'rb') as uaf:
            for ua in uaf.readlines():
                if ua:
                    uas.append(ua.strip()[:-1])
        random.shuffle(uas)
        return uas

json写入中文为乱码

with open('data.json', 'w', encoding='utf-8') as file:
    file.write(json.dumps(data, indent=2, ensure_ascii=False))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值