Py学习  »  Python

那些有趣\/用的 Python 库

机器学习算法与Python学习 • 4 年前 • 504 次点击  

来源:苏生不惑    编辑:Python开发者

segmentfault.com/a/1190000010103386


图片处理

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


youtube-dl下载国外视频

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


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

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



Python 玩转网易云音乐

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


下载视频字幕

1pip install getsub



Python 财经数据接口包

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



开源漏洞靶场

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


北京实时公交

 1pip install -r requirements.txt 安装依赖
2
3python 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[122(农业展览馆-华纺易城公交场站)>, 101(广顺南大街北口-蓝龙家园)>, ...]
14
15>>> lines = BeijingBus.search_lines( 847 )
16
17>>> lines
18
19[847(马甸桥西-雷庄村)>, 847(雷庄村-马甸桥西)>]
20
21>>> line = lines[0]
22
23>>> print line.id, line.name
24
25541 847(马甸桥西-雷庄村)
26
27>>> line.stations
28
29[, ...]
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
43         id : 公交车id,
44
45         lat : 公交车的位置,
46
47         lon : 公交车位置,
48
49         next_station_name : 下一站的名字,
50
51         next_station_num : 下一站的序号,
52
53         next_station_distance : 离下一站的距离,
54
55         next_station_arriving_time : 预计到达下一站的时间,
56
57         station_distance : 离本站的距离,
58
59         station_arriving_time : 预计到达本站的时间,
60
61    },
62
63    ...
64
65]



文章提取器

 1git clone https://github.com/grangier/python-goose.git
2
3cd python-goose
4
5pip install -r requirements.txt
6
7python 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]
22
23香港行政长官梁振英在各方压力下就其大宅的违章建筑(僭建)问题到立法会接受质询,并向香港民众道歉。
24
25
26
27 梁振英在星期二(1210日)的答问大会开始之际在其演说中道歉,但强调他在违章建筑问题上没有隐瞒的意图和动机。
28
29
30
31一些亲北京阵营议员欢迎梁振英道歉,且认为应能获得香港民众接受,但这些议员也质问梁振英有



Python 艺术二维码生成器

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


伪装浏览器身份

 1pip install fake-useragent
2
3from fake_useragent import UserAgent
4
5ua = UserAgent()
6
7
8
9ua.ie
10
11# Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
12
13ua.msie
14
15# Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)
16
17ua[ 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
21ua.opera
22
23# Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11
24
25ua.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


pip install httpstat

httpstat httpbin.org/get



python shell


pip install sh

from sh import ifconfig

print ifconfig("eth0")


处理中文文本内容


pip install -U textblob#英文文本的情感分析

pip install snownlp#中文文本的情感分析

from snownlp import SnowNLP

text = "I am happy today. I feel sad today."

from textblob import TextBlob

blob = TextBlob(text)

TextBlob("I am happy today. I feel sad today.")

blob.sentiment

Sentiment(polarity=0.15000000000000002, subjectivity=1.0)

 

 

s = SnowNLP(u 这个东西真心很赞 )

 

s.words         # [u 这个 , u 东西 , u 真心 ,

                #  u 很 , u 赞 ]

 

s .tags          # [(u 这个 , u r ), (u 东西 , u n ),

                #  (u 真心 , u d ), (u 很 , u d ),

                #  (u 赞 , u Vg )]

 

s.sentiments    # 0.9769663402895832 positive的概率

 

s.pinyin        # [u zhe , u ge , u dong , u xi ,

                #  u zhen , u xin , u hen , u zan ]

 

s = SnowNLP(u 「繁體字」「繁體中文」的叫法在臺灣亦很常見。)

 

s.han           # u 「繁体字」「繁体中文」的叫法

                # 在台湾亦很常见。


抓取发放代理


pip install -U getproxy

~ getproxy --help

Usage: getproxy [OPTIONS]

 

Options:

--in-proxy TEXT Input proxy file

--out-proxy TEXT Output proxy file

--help Show this message and exit.


  • --in-proxy 可选参数,待验证的 proxies 列表文件

  • --out-proxy 可选参数,输出已验证的 proxies 列表文件,如果为空,则直接输出到终端

  • --in-proxy 文件格式和 --out-proxy 文件格式一致


zhihu api


pip install git+git://github.com/lzjun567/zhihu-api --upgrade

from zhihu import Zhihu

zhihu = Zhihu()

zhihu.user(user_slug ="xiaoxiaodouzi")

 

{ avatar_url_template : http://img2.jintiankansha.me/get2?src=http://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_{size}.jpg ,

     badge : [],

     name : 我是小号 ,

     headline : 程序员 ,

     gender : -1,

     user_type : people ,

     is_advertiser : False,

     avatar_url : http://img2.jintiankansha.me/get2?src=http://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_is.jpg ,

     url : http://www.zhihu.com/api/v4/people/1da75b85900e00adb072e91c56fd9149 , type : people ,

     url_token : xiaoxiaodouzi ,

     id : 1da75b85900e00adb072e91c56fd9149 ,

     is_org : False}


Python 密码泄露查询模块


pip install leakPasswd

import leakPasswd

leakPasswd.findBreach( taobao )



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


pip install ngxtop

$ ngxtop

running for 411 seconds, 64332 records processed: 156.60 req/sec

 

Summary:

|   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |

|---------+------------------+-------+-------+-------+-------|

|   64332 |         2775.251 | 61262 |  2994 |    71 |     5 |

 

Detailed:

| request_path                             |   count |   avg_bytes_sent |   2xx |   3xx |   4xx |   5xx |

|------------------------------------------+---------+------------------+-------+-------+-------+-------|

| /abc/xyz/xxxx                            |   20946 |          434.693 | 20935 |     0 |    11 |     0 |

| /xxxxx.json                               |    5633 |         1483.723 |  5633 |     0 |     0 |     0 |

| /xxxxx/xxx/xxxxxxxxxxxxx                 |    3629 |         6835.499 |  3626 |     0 |     3 |     0 |

| /xxxxx/xxx /xxxxxxxx                      |    3627 |        15971.885 |  3623 |     0 |     4 |     0 |

| /xxxxx/xxx/xxxxxxx                       |    3624 |         7830.236 |  3621 |     0 |     3 |     0 |

| /static /js/minified/utils.min.js         |    3031 |         1781.155 |  2104 |   927 |     0 |     0 |

| /static/js/minified/xxxxxxx.min.v1.js     |    2889 |         2210.235 |  2068 |   821 |     0 |     0 |

| /static/tracking/js/xxxxxxxx.js          |    2594 |         1325.681 |  1927 |   667 |     0 |     0 |

| /xxxxx/xxx.html                          |    2521 |          573.597 |  2520 |     0 |     1 |     0 |

| /xxxxx/xxxx.json                         |    1840 |          800.542 |  1839 |     0 |     1 |     0 |


火车余票查询


pip install iquery

Usage:

        iquery (-c|彩票)

        iquery (-m|电影)

        iquery -p <city>

        iquery -l song [singer]

        iquery -p <city> <hospital>

        iquery <city> <show> [<days>]

        iquery [-dgktz] <from> <to > <date>

 

    Arguments:

        from             出发站

        to               到达站

        date             查询日期

 

        city             查询城市

        show             演出的类型

        days             查询近()天内的演出, 若省略, 默认15

 

        city             城市名,加在-p后查询该城市所有莆田医院

        hospital         医院名,加在city后检查该医院是否是莆田系

 

 

    Options:

        -h, --help       显示该帮助菜单.

        -dgktz           动车,高铁,快速,特快,直达

        -m               热映电影查询

        -p               莆田系医院查询

        -l               歌词查询

        -c               彩票查询

 

    Show:

        演唱会 音乐会 音乐剧 歌舞剧 儿童剧 话剧

        歌剧 比赛 舞蹈 戏曲 相声 杂技 马戏 魔术

推荐阅读

80后博导拟任“211大学”副校长 曾是人大最年轻教授

史上最全 Python Re 模块讲解

这些用 Python 写的牛逼程序/脚本,你玩过吗?

大四本科生发明AI论文生成器,写出的论文达到几近完善程度

2019年6月GitHub上最热门的开源项目!

一键脱衣AI原理解密:开源算法,英伟达伯克利研究,不高深也不神秘


喜欢就点击“在看”吧!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/35685
 
504 次点击