点击网页特效
一个点击网页出现爱心特效的插件 click_heart.js ,当然大家可能也见过其他博客上面,有点击网页出现类似 富强、民主、文明、和谐等等,关于代码在这里不多赘述,网上一查就能查到。代码如下:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> /*鼠标点击出现文字*/ $(document).ready(function () { var a_index = 0; $("body").click(function (e) { var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善"); var $i = $("<span/>").text(a[a_index]); a_index = (a_index + 1) % a.length; var x = e.pageX, y = e.pageY; $i.css({ "z-index": 99999, "top": y - 20, "left": x, "position": "absolute", "font-weight": "bold", "font-size": "12px", "color": "#ff6651" }); $("body").append($i); $i.animate({"top": y - 180, "opacity": 0}, 1500, function () { $i.remove(); }); }); }); </script> <style> div { width: 200px; height: 200px; margin: 0 auto; background-color: rgba(133, 132, 132, .2); text-align: center; } </style> </head> <body> <div></div> </body>
使用效果如下:
点击特效
那么,既然周末闲来无事,不如给自己找点乐子吧!
获取Python模块
如何快速获取电脑已安装的Python模块呢?方法有很多:
- 打开cmd,输入python进入python命令行,然后输入
help("modules")
命令行查看模块
- 通过代码查询Python内置的模块
import sys sys.modules.keys() # dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'nt', 'winreg', 'encodings', 'codecs', '_codecs', 'encodings.aliases', ...
- 输入
pip list
可以查看通过pip 安装过的所有模块
查看pip安装的模块
冷门知识点
这里介绍一个冷门知识点,可能面试的时候,遇到坑人的面试官会用到哦,大家可要记好了。
在刚才我们打印的内置模块中,有很多是_开头的模块,这些是干嘛的,你知道吗?
大家了解我们日常学习的Python其实最广泛是通过C语言实现的Python解释器,即Cpython,当然还有Jpython、ironpython等等。那么这些下划线开头的模块,绝大多数都是代表这个模块时通过C语言编写的扩展,然后编译到Python中的,所以这些模块其实是一个连接库,并不是.py文件。
当然他们编译后的存储也是有所不同的,有些作为内置的Builtin,而有些作为文件保存。即便是作为文件,名称也是有所不同的,在windows下他们可能叫xxx.pyd,而在Linux下他们绝大多数是xxx.so文件。
比如Python自带的GUI模块tkinter,让我们看看下面这段代码输出:
import _tkinter print(_tkinter.__file__) # E:\Software\Python37\DLLs\_tkinter.pyd
这么冷门的知识,大家学废了么,以后关键时刻装X就靠它了!
代码获取pip安装的模块
回归我们页面点击的正题,既然我们是Python博客,那么什么点击爱心和标语明显不适合我们,让用户点击出现Python的模块岂不是很赞?说干就干走起。
刚才我们看到,上面那段js,显示的内容都在那段Array内,我们只需要准备好那一堆字符串即可,但是我们如果只导入sys.modules,那仅仅能获取系统内置的所有模块,我们安装的模块该如何获取呢?
难道只能用subprocess 执行pip list
命令解析结果?这里找了很多资料,研究结果如下:
# python2 import pip pip.get_installed_distributions() # python3 版本get_installed_distributions方法藏得很深 from pip._internal.utils.misc import get_installed_distributions get_installed_distributions() # 最终代码: from pip._internal.utils.misc import get_installed_distributions for module_name in get_installed_distributions(): print(module_name.key) # output: -ip you-get wtforms werkzeug webassets watchdog urllib3 texttable sqlalchem ...
获取全量模块
既然能得到内部模块+pip安装的模块,那么我们追加两者的结果就可以获取我们本机所有安装的模块了。但是这里有一个小坑
一定要等我们sys.modules数据获取完成后,再导入pip否则会将pip的子类全部追加到modules中去!
最终代码如下:
# -*- coding: utf-8 -*- # @Author : 王翔 # @微信号 : King_Uranus # @公众号 : 清风Python # @GitHub : https://github.com/BreezePython # @Date : 2021/01/09 23:54:47 # @Software : PyCharm # @version :Python 3.7.3 # @File : click_modules.py import sys # 提供我们将模块列表转化为字符串所需 import json # from pip._internal.utils.misc import get_installed_distributions # 过滤掉那些 _开头的C语言链接库 modules = [name for name in sys.modules if not name.startswith('_')] print(len(modules)) # 53个 # 等待获取完成后再导入pip,否则内置模块编程了177个 from pip._internal.utils.misc import get_installed_distributions for module_name in get_installed_distributions(): modules.append(module_name.key) print(len(modules)) # 还以为自己用了很多,原来全量才135个啊... print(json.dumps(sorted(modules))) # output: # ["-ip", "abc", "alabaster", "alembic", "altgraph", "babel", "baidu-aip", "blinker", "builtins", "certifi", "chardet", "click", "codecs", "collections", "colorama", "commonmark", "contextlib", "copyreg", "cssmin", "docutils", "encodings", "encodings.aliases", "encodings.gbk", "encodings.latin_1", "encodings.utf_8", "enum", "flask", "flask-assets", "flask-caching", "flask-ckeditor", "flask-debugtoolbar", "flask-dropzone", "flask-mail", "flask-migrate", "flask-sqlalchemy", "flask-wtf", "functools", "future", "genericpath", "heapq", "idna", "imageio", "imagesize", "importlib", "importlib._bootstrap", "importlib._bootstrap_external", "importlib.abc", "importlib.machinery", "importlib.util", "install", "io", "itertools", "itsdangerous", "jinja2", "jsmin", "json", "json.decoder", "json.encoder", "json.scanner", "keyboard", "keyword", "linecache", "lxml", "mako", "markupsafe", "marshal", "myqr", "nt", "ntpath", "numpy", "operator", "os", "os.path", "packaging", "pathtools", "pefile", "pillow", "pip", "py7zr", "pycryptodome", "pyfiglet", "pygments", "pyinstaller", "pyparsing", "python-dateutil", "python-dotenv", "python-editor", "python-http-client", "pytz", "pywin32-ctypes", "pyyaml", "pyzbar", "re", "recommonmark", "redis", "reprlib", "requests", "ruamel", "ruamel.yaml", "ruamel.yaml.clib", "sendgrid", "setuptools", "site", "sitecustomize", "six", "snowballstemmer", "sphinx", "sphinx-rtd-theme", "sphinxcontrib", "sphinxcontrib-applehelp", "sphinxcontrib-devhelp", "sphinxcontrib-htmlhelp", "sphinxcontrib-jsmath", "sphinxcontrib-qthelp", "sphinxcontrib-serializinghtml", "sqlalchemy", "sre_compile", "sre_constants", "sre_parse", "stat", "sys", "texttable", "token", "tokenize", "traceback", "types", "urllib3", "warnings", "watchdog", "webassets", "werkzeug", "winreg", "wtforms", "you-get", "zipimport"]
简单几行代码,完成我们的模块导出诉求。现在把这些模块,添加当刚才的js中,然后验证下效果,来看看钢琴手卡尔的点击速度吧,哈哈:
Python模块点击特效
我们把这样的点击特效放到自己博客上,才能算是Pythoner该有的样子啊!大家觉得有道理吗?哈哈...