那些有趣/用的Python库

简介:

图片处理


 
 
  1. pip install pillow 
  2.  
  3. from PIL import Image 
  4.  
  5. import numpy as np 
  6.  
  7. a = np.array(Image.open('test.jpg')) 
  8.  
  9. b = [255,255,255] - a 
  10.  
  11. im = Image.fromarray(b.astype('uint8')) 
  12.  
  13. im.save('new.jpg')  

youtube-dl下载国外视频


 
 
  1. pip install youtube-dl #直接安装youtube-dl 
  2.  
  3. pip install -U youtube-dl #安装youtube-dl并更新 
  4.  
  5. youtube-dl "http://www.youtube.com/watch?v=-wNyEUrxzFU"  

查看对象的全部属性和方法


 
 
  1. pip install pdir2 
  2.  
  3. >>> import pdir,requests 
  4.  
  5. >>> pdir(requests) 
  6.  
  7. module attribute: 
  8.  
  9.     __cached__, __file__, __loader__, __name__, __package__, __path__, __spec__ 
  10.  
  11. other: 
  12.  
  13.     __author__, __build__, __builtins__, __copyright__, __license__, __title__, 
  14.  
  15. __version__, _internal_utils, adapters, api, auth, certs, codes, compat, cookies 
  16.  
  17. , exceptions, hooks, logging, models, packages, pyopenssl, sessions, status_code 
  18.  
  19. s, structures, utils, warnings 
  20.  
  21. special attribute: 
  22.  
  23.     __doc__ 
  24.  
  25. class: 
  26.  
  27.     NullHandler: This handler does nothing. It's intended to be used to avoid th 
  28.  
  29.  
  30.     PreparedRequest: The fully mutable :class:`PreparedRequest <PreparedRequest> 
  31.  
  32. ` object, 
  33.  
  34.     Request: A user-created :class:`Request <Request>` object. 
  35.  
  36.     Response: The :class:`Response <Response>` object, which contains a 
  37.  
  38.     Session: A Requests session. 
  39.  
  40. exception: 
  41.  
  42.     ConnectTimeout: The request timed out while trying to connect to the remote 
  43.  
  44. server. 
  45.  
  46.     ConnectionError: A Connection error occurred. 
  47.  
  48.     DependencyWarning: Warned when an attempt is made to import a module with mi 
  49.  
  50. ssing optional 
  51.  
  52.     FileModeWarning: A file was opened in text mode, but Requests determined its 
  53.  
  54. binary length. 
  55.  
  56.     HTTPError: An HTTP error occurred. 
  57.  
  58.     ReadTimeout: The server did not send any data in the allotted amount of time 
  59.  
  60.  
  61.     RequestException: There was an ambiguous exception that occurred while handl 
  62.  
  63. ing your 
  64.  
  65.     Timeout: The request timed out
  66.  
  67.     TooManyRedirects: Too many redirects. 
  68.  
  69.     URLRequired: A valid URL is required to make a request. 
  70.  
  71. function
  72.  
  73.     delete: Sends a DELETE request. 
  74.  
  75.     get: Sends a GET request. 
  76.  
  77.     head: Sends a HEAD request. 
  78.  
  79.     options: Sends a OPTIONS request. 
  80.  
  81.     patch: Sends a PATCH request. 
  82.  
  83.     post: Sends a POST request. 
  84.  
  85.     put: Sends a PUT request. 
  86.  
  87.     request: Constructs and sends a :class:`Request <Request>`. 
  88.  
  89.     session: Returns a :class:`Session` for context-management.  

Python 玩转网易云音乐


 
 
  1. pip install ncmbot 
  2.  
  3. import ncmbot 
  4.  
  5. #登录 
  6.  
  7. bot = ncmbot.login(phone='xxx'password='yyy'
  8.  
  9. bot.content # bot.json() 
  10.  
  11. #获取用户歌单 
  12.  
  13. ncmbot.user_play_list(uid='36554272')  

下载视频字幕


 
 
  1. pip install getsub 

Python 财经数据接口包


 
 
  1. pip install tushare 
  2.  
  3. import tushare as ts 
  4.  
  5. #一次性获取最近一个日交易日所有股票的交易数据 
  6.  
  7. ts.get_today_all() 
  8.  
  9.   
  10.  
  11. 代码,名称,涨跌幅,现价,开盘价,最高价,最低价,最日收盘价,成交量,换手率 
  12.  
  13.       code    name     changepercent  trade   open   high    low  settlement \   
  14.  
  15. 0     002738  中矿资源         10.023  19.32  19.32  19.32  19.32       17.56   
  16.  
  17. 1     300410  正业科技         10.022  25.03  25.03  25.03  25.03       22.75   
  18.  
  19. 2     002736  国信证券         10.013  16.37  16.37  16.37  16.37       14.88   
  20.  
  21. 3     300412  迦南科技         10.010  31.54  31.54  31.54  31.54       28.67   
  22.  
  23. 4     300411  金盾股份         10.007  29.68  29.68  29.68  29.68       26.98   
  24.  
  25. 5     603636  南威软件         10.006  38.15  38.15  38.15  38.15       34.68   
  26.  
  27. 6     002664  信质电机         10.004  30.68  29.00  30.68  28.30       27.89   
  28.  
  29. 7     300367  东方网力         10.004  86.76  78.00  86.76  77.87       78.87   
  30.  
  31. 8     601299  中国北车         10.000  11.44  11.44  11.44  11.29       10.40   
  32.  
  33. 9     601880   大连港         10.000   5.72   5.34   5.72   5.22        5.20   
  34.  
  35. 10    000856  冀东装备         10.000   8.91   8.18   8.91   8.18        8.10  

开源漏洞靶场


 
 
  1. # 安装pip 
  2.  
  3. curl -s https://bootstrap.pypa.io/get-pip.py | python3 
  4.  
  5.   
  6.  
  7. # 安装docker 
  8.  
  9. apt-get update && apt-get install docker.io 
  10.  
  11.   
  12.  
  13. # 启动docker服务 
  14.  
  15. service docker start 
  16.  
  17.   
  18.  
  19. # 安装compose 
  20.  
  21. pip install docker-compose 
  22.  
  23. # 拉取项目 
  24.  
  25. git clone git@github.com:phith0n/vulhub.git 
  26.  
  27. cd vulhub 
  28.  
  29.   
  30.  
  31. # 进入某一个漏洞/环境的目录 
  32.  
  33. cd nginx_php5_mysql 
  34.  
  35.   
  36.  
  37. # 自动化编译环境 
  38.  
  39. docker-compose build 
  40.  
  41.   
  42.  
  43. # 启动整个环境 
  44.  
  45. docker-compose up -d 
  46.  
  47. #测试完成后,删除整个环境 
  48.  
  49. docker-compose down  

北京实时公交


 
 
  1. pip install -r requirements.txt 安装依赖 
  2.  
  3. python manage.py build_cache 获取离线数据,建立本地缓存 
  4.  
  5. #项目自带了一个终端中的查询工具作为例子,运行: python manage.py cli 
  6.  
  7. >>> from beijing_bus import BeijingBus 
  8.  
  9. >>> lines = BeijingBus.get_all_lines() 
  10.  
  11. >>> lines 
  12.  
  13. [<Line: 运通122(农业展览馆-华纺易城公交场站)>, <Line: 运通101(广顺南大街北口-蓝龙家园)>, ...] 
  14.  
  15. >>> lines = BeijingBus.search_lines('847'
  16.  
  17. >>> lines 
  18.  
  19. [<Line: 847(马甸桥西-雷庄村)>, <Line: 847(雷庄村-马甸桥西)>] 
  20.  
  21. >>> line = lines[0] 
  22.  
  23. >>> print line.id, line.name 
  24.  
  25. 541 847(马甸桥西-雷庄村) 
  26.  
  27. >>> line.stations 
  28.  
  29. [<Station 马甸桥西>, <Station 马甸桥东>, <Station 安华桥西>, ...] 
  30.  
  31. >>> station = line.stations[0] 
  32.  
  33. >>> print station.name, station.lat, station.lon 
  34.  
  35. 马甸桥西 39.967721 116.372921 
  36.  
  37. >>> line.get_realtime_data(1) # 参数为站点的序号,从1开始 
  38.  
  39.  
  40.     { 
  41.  
  42.         'id': 公交车id, 
  43.  
  44.         'lat': 公交车的位置, 
  45.  
  46.         'lon': 公交车位置, 
  47.  
  48.         'next_station_name': 下一站的名字, 
  49.  
  50.         'next_station_num': 下一站的序号, 
  51.  
  52.         'next_station_distance': 离下一站的距离, 
  53.  
  54.         'next_station_arriving_time': 预计到达下一站的时间, 
  55.  
  56.         'station_distance': 离本站的距离, 
  57.  
  58.         'station_arriving_time': 预计到达本站的时间, 
  59.  
  60.     }, 
  61.  
  62.     ... 
  63.  
  64. ]  

文章提取器


 
 
  1. git clone https://github.com/grangier/python-goose.git 
  2.  
  3. cd python-goose 
  4.  
  5. pip install -r requirements.txt 
  6.  
  7. python setup.py install 
  8.  
  9.   
  10.  
  11. >>> from goose import Goose 
  12.  
  13. >>> from goose.text import StopWordsChinese 
  14.  
  15. >>> url  = 'http://www.bbc.co.uk/zhongwen/simp/chinese_news/2012/12/121210_hongkong_politics.shtml' 
  16.  
  17. >>> g = Goose({'stopwords_class': StopWordsChinese}) 
  18.  
  19. >>> article = g.extract(url=url) 
  20.  
  21. >>> print article.cleaned_text[:150]  

香港行政长官梁振英在各方压力下就其大宅的违章建筑(僭建)问题到立法会接受质询,并向香港民众道歉。

梁振英在星期二(12月10日)的答问大会开始之际在其演说中道歉,但强调他在违章建筑问题上没有隐瞒的意图和动机。

一些亲北京阵营议员欢迎梁振英道歉,且认为应能获得香港民众接受,但这些议员也质问梁振英有

Python 艺术二维码生成器


 
 
  1. pip install MyQR 
  2.  
  3. myqr https://github.com 
  4.  
  5. myqr https://github.com -v 10 -l Q 

伪装浏览器身份


 
 
  1. pip install fake-useragent 
  2.  
  3. from fake_useragent import UserAgent 
  4.  
  5. ua = UserAgent() 
  6.  
  7.   
  8.  
  9. ua.ie 
  10.  
  11. # Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US); 
  12.  
  13. ua.msie 
  14.  
  15. # Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)' 
  16.  
  17. ua['Internet Explorer'
  18.  
  19. # Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US) 
  20.  
  21. ua.opera 
  22.  
  23. # Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11 
  24.  
  25. ua.chrome 
  26.  
  27. # Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'  

美化 curl


 
 
  1. pip install httpstat 
  2.  
  3. httpstat httpbin.org/get 


 
 
  1. python shell 
  2.  
  3. pip install sh 
  4.  
  5. from sh import ifconfig 
  6.  
  7. print ifconfig("eth0")  

处理中文文本内容


 
 
  1. pip install -U textblob#英文文本的情感分析 
  2.  
  3. pip install snownlp#中文文本的情感分析 
  4.  
  5. from snownlp import SnowNLP 
  6.  
  7. text = "I am happy today. I feel sad today." 
  8.  
  9. from textblob import TextBlob 
  10.  
  11. blob = TextBlob(text) 
  12.  
  13. TextBlob("I am happy today. I feel sad today."
  14.  
  15. blob.sentiment 
  16.  
  17. Sentiment(polarity=0.15000000000000002, subjectivity=1.0) 
  18.  
  19.   
  20.  
  21.   
  22.  
  23. s = SnowNLP(u'这个东西真心很赞'
  24.  
  25.   
  26.  
  27. s.words         # [u'这个', u'东西', u'真心'
  28.  
  29.                 #  u'很', u'赞'
  30.  
  31.   
  32.  
  33. s.tags          # [(u'这个', u'r'), (u'东西', u'n'), 
  34.  
  35.                 #  (u'真心', u'd'), (u'很', u'd'), 
  36.  
  37.                 #  (u'赞', u'Vg')] 
  38.  
  39.   
  40.  
  41. s.sentiments    # 0.9769663402895832 positive的概率 
  42.  
  43.   
  44.  
  45. s.pinyin        # [u'zhe', u'ge', u'dong', u'xi'
  46.  
  47.                 #  u'zhen', u'xin', u'hen', u'zan'
  48.  
  49.   
  50.  
  51. s = SnowNLP(u'「繁體字」「繁體中文」的叫法在臺灣亦很常見。'
  52.  
  53.   
  54.  
  55. s.han           # u'「繁体字」「繁体中文」的叫法 
  56.  
  57.                 # 在台湾亦很常见。  

抓取发放代理


 
 
  1. pip install -U getproxy 
  2.  
  3.  ~ getproxy --help 
  4.  
  5. Usage: getproxy [OPTIONS] 
  6.  
  7.   
  8.  
  9. Options: 
  10.  
  11. --in-proxy TEXT Input proxy file 
  12.  
  13. --out-proxy TEXT Output proxy file 
  14.  
  15. --help Show this message and exit. 
  • --in-proxy 可选参数,待验证的 proxies 列表文件
  • --out-proxy 可选参数,输出已验证的 proxies 列表文件,如果为空,则直接输出到终端
  • --in-proxy 文件格式和 --out-proxy 文件格式一致

zhihu api


 
 
  1. pip install git+git://github.com/lzjun567/zhihu-api --upgrade 
  2.  
  3. from zhihu import Zhihu 
  4.  
  5. zhihu = Zhihu() 
  6.  
  7. zhihu.user(user_slug="xiaoxiaodouzi"
  8.  
  9.   
  10.  
  11. {'avatar_url_template''https://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_{size}.jpg'
  12.  
  13.      'badge': [], 
  14.  
  15.      'name''我是小号'
  16.  
  17.      'headline''程序员'
  18.  
  19.      'gender': -1, 
  20.  
  21.      'user_type''people'
  22.  
  23.      'is_advertiser'False
  24.  
  25.      'avatar_url''https://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_is.jpg'
  26.  
  27.      'url''http://www.zhihu.com/api/v4/people/1da75b85900e00adb072e91c56fd9149''type''people'
  28.  
  29.      'url_token''xiaoxiaodouzi'
  30.  
  31.      'id''1da75b85900e00adb072e91c56fd9149'
  32.  
  33.      'is_org'False

解析 nginx 访问日志并格式化输出


 
 
  1. pip install ngxtop 
  2.  
  3. $ ngxtop 
  4.  
  5. running for 411 seconds, 64332 records processed: 156.60 req/sec 
  6.  
  7.   
  8.  
  9. Summary: 
  10.  
  11. |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx | 
  12.  
  13. |---------+------------------+-------+-------+-------+-------| 
  14.  
  15. |   64332 |         2775.251 | 61262 |  2994 |    71 |     5 | 
  16.  
  17.   
  18.  
  19. Detailed: 
  20.  
  21. | request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx | 
  22.  
  23. |------------------------------------------+---------+------------------+-------+-------+-------+-------| 
  24.  
  25. | /abc/xyz/xxxx                            |   20946 |          434.693 | 20935 |     0 |    11 |     0 | 
  26.  
  27. | /xxxxx.json                              |    5633 |         1483.723 |  5633 |     0 |     0 |     0 | 
  28.  
  29. | /xxxxx/xxx/xxxxxxxxxxxxx                 |    3629 |         6835.499 |  3626 |     0 |     3 |     0 | 
  30.  
  31. | /xxxxx/xxx/xxxxxxxx                      |    3627 |        15971.885 |  3623 |     0 |     4 |     0 | 
  32.  
  33. | /xxxxx/xxx/xxxxxxx                       |    3624 |         7830.236 |  3621 |     0 |     3 |     0 | 
  34.  
  35. | /static/js/minified/utils.min.js         |    3031 |         1781.155 |  2104 |   927 |     0 |     0 | 
  36.  
  37. | /static/js/minified/xxxxxxx.min.v1.js    |    2889 |         2210.235 |  2068 |   821 |     0 |     0 | 
  38.  
  39. | /static/tracking/js/xxxxxxxx.js          |    2594 |         1325.681 |  1927 |   667 |     0 |     0 | 
  40.  
  41. | /xxxxx/xxx.html                          |    2521 |          573.597 |  2520 |     0 |     1 |     0 | 
  42.  
  43. | /xxxxx/xxxx.json                         |    1840 |          800.542 |  1839 |     0 |     1 |     0 |  

火车余票查询


 
 
  1. pip install iquery  
  2. Usage: 
  3.  
  4.         iquery (-c|彩票) 
  5.  
  6.         iquery (-m|电影) 
  7.  
  8.         iquery -p <city> 
  9.  
  10.         iquery -l song [singer] 
  11.  
  12.         iquery -p <city> <hospital> 
  13.  
  14.         iquery <city> <show> [<days>] 
  15.  
  16.         iquery [-dgktz] <from> <to> <date
  17.  
  18.   
  19.  
  20.     Arguments: 
  21.  
  22.         from             出发站 
  23.  
  24.         to               到达站 
  25.  
  26.         date             查询日期 
  27.  
  28.   
  29.  
  30.         city             查询城市 
  31.  
  32.         show             演出的类型 
  33.  
  34.         days             查询近(几)天内的演出, 若省略, 默认15 
  35.  
  36.   
  37.  
  38.         city             城市名,加在-p后查询该城市所有莆田医院 
  39.  
  40.         hospital         医院名,加在city后检查该医院是否是莆田系 
  41.  
  42.   
  43.  
  44.   
  45.  
  46.     Options: 
  47.  
  48.         -h, --help       显示该帮助菜单. 
  49.  
  50.         -dgktz           动车,高铁,快速,特快,直达 
  51.  
  52.         -m               热映电影查询 
  53.  
  54.         -p               莆田系医院查询 
  55.  
  56.         -l               歌词查询 
  57.  
  58.         -c               彩票查询 
  59.  
  60.   
  61.  
  62.     Show: 
  63.  
  64.         演唱会 音乐会 音乐剧 歌舞剧 儿童剧 话剧 
  65.  
  66.         歌剧 比赛 舞蹈 戏曲 相声 杂技 马戏 魔术  



作者:佚名

来源:51CTO

相关文章
|
2月前
|
机器学习/深度学习 存储 数据挖掘
Python图像处理实用指南:PIL库的多样化应用
本文介绍Python中PIL库在图像处理中的多样化应用,涵盖裁剪、调整大小、旋转、模糊、锐化、亮度和对比度调整、翻转、压缩及添加滤镜等操作。通过具体代码示例,展示如何轻松实现这些功能,帮助读者掌握高效图像处理技术,适用于图片美化、数据分析及机器学习等领域。
93 20
|
1月前
|
数据采集 JavaScript Android开发
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
55 7
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
2月前
|
测试技术 Python
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
161 31
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
|
4月前
|
调度 开发者 Python
Python中的异步编程:理解asyncio库
在Python的世界里,异步编程是一种高效处理I/O密集型任务的方法。本文将深入探讨Python的asyncio库,它是实现异步编程的核心。我们将从asyncio的基本概念出发,逐步解析事件循环、协程、任务和期货的概念,并通过实例展示如何使用asyncio来编写异步代码。不同于传统的同步编程,异步编程能够让程序在等待I/O操作完成时释放资源去处理其他任务,从而提高程序的整体效率和响应速度。
|
3月前
|
XML JSON 数据库
Python的标准库
Python的标准库
197 77
|
23天前
|
Web App开发 数据采集 数据安全/隐私保护
Selenium库详解:Python实现模拟登录与反爬限制的进阶指南
Selenium库详解:Python实现模拟登录与反爬限制的进阶指南
|
4月前
|
机器学习/深度学习 算法 数据挖掘
数据分析的 10 个最佳 Python 库
数据分析的 10 个最佳 Python 库
267 4
数据分析的 10 个最佳 Python 库
|
4月前
|
XML 存储 数据库
Python中的xmltodict库
xmltodict是Python中用于处理XML数据的强大库,可将XML数据与Python字典相互转换,适用于Web服务、配置文件读取及数据转换等场景。通过`parse`和`unparse`函数,轻松实现XML与字典间的转换,支持复杂结构和属性处理,并能有效管理错误。此外,还提供了实战案例,展示如何从XML配置文件中读取数据库连接信息并使用。
Python中的xmltodict库
|
4月前
|
人工智能 API 开发工具
aisuite:吴恩达发布开源Python库,一个接口调用多个大模型
吴恩达发布的开源Python库aisuite,提供了一个统一的接口来调用多个大型语言模型(LLM)服务。支持包括OpenAI、Anthropic、Azure等在内的11个模型平台,简化了多模型管理和测试的工作,促进了人工智能技术的应用和发展。
276 1
aisuite:吴恩达发布开源Python库,一个接口调用多个大模型
|
3月前
|
XML JSON 数据库
Python的标准库
Python的标准库
90 11

热门文章

最新文章