那些有趣/用的Python库

开发 后端
本文介绍一些有趣、有用的Python库,其中包括图片处理、youtube-dl下载国外视频、查看对象的全部属性和方法等。

图片处理

  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.         歌剧 比赛 舞蹈 戏曲 相声 杂技 马戏 魔术  
责任编辑:庞桂玉 来源: Python开发者
相关推荐

2022-12-28 08:59:11

2010-04-09 11:07:40

Oracle 有趣排序

2011-06-13 10:30:07

Linus Torva

2020-07-13 07:27:16

Python开发

2022-08-23 12:32:37

Python可视化图表

2013-01-14 10:23:32

2022-03-31 11:56:34

Android 12智能手机功能

2022-06-30 09:00:23

Vue.js版本名称

2021-07-13 10:59:49

Python可视化

2012-02-20 10:14:37

科技记者软件

2018-09-08 17:45:26

2020-07-29 10:00:38

PythonEllipsis索引

2022-10-10 23:19:02

Python脚本语言工具库

2020-05-28 13:11:37

Python编码 开发

2020-03-29 20:27:51

Python函数开发

2022-01-23 14:08:52

PythonPygame

2016-12-05 15:15:52

JavaScriptCSS库

2019-10-31 08:59:23

Python工具万圣节

2018-11-30 18:30:17

数据库运维机器学习

2017-07-12 10:25:17

Python微信itchat 包
点赞
收藏

51CTO技术栈公众号