那些有趣/用的 Python 庫
(點擊
上方藍字
,快速關注我們)
來源:蘇生不惑
segmentfault.com/a/1190000010103386
如有好文章投稿,請點擊 → 這裡了解詳情
圖片處理
pip install pillow
from
PIL
import
Image
import
numpy
as
np
a
=
np
.
array
(
Image
.
open
(
"test.jpg"
))
b
=
[
255
,
255
,
255
]
-
a
im
=
Image
.
fromarray
(
b
.
astype
(
"uint8"
))
im
.
save
(
"new.jpg"
)
youtube-dl下載國外視頻
pip install
youtube
-
dl
#直接安裝youtube-dl
pip
install
-
U
youtube
-
dl
#安裝youtube-dl並更新
youtube
-
dl
"http://www.youtube.com/watch?v=-wNyEUrxzFU"
查看對象的全部屬性和方法
pip install
pdir2
>>>
import
pdir
,
requests
>>>
pdir
(
requests
)
module
attribute
:
__cached__
,
__file__
,
__loader__
,
__name__
,
__package__
,
__path__
,
__spec__
other
:
__author__
,
__build__
,
__builtins__
,
__copyright__
,
__license__
,
__title__
,
__version__
,
_internal_utils
,
adapters
,
api
,
auth
,
certs
,
codes
,
compat
,
cookies
,
exceptions
,
hooks
,
logging
,
models
,
packages
,
pyopenssl
,
sessions
,
status
_
codes
,
structures
,
utils
,
warnings
special
attribute
:
__doc__
class
:
NullHandler
:
This handler does
nothing
.
It
"s
intended to be used to avoid
th
e
PreparedRequest
:
The fully
mutable
:
class
:
`
PreparedRequest
<
PreparedRequest
>
`
object
,
Request
:
A
user
-
created
:
class
:
`
Request
<
Request
>
`
object
.
Response
:
The
:
class
:
`
Response
<
Response
>
`
object
,
which
contains
a
Session
:
A
Requests
session
.
exception
:
ConnectTimeout
:
The request timed out
while
trying to connect to the remote
server
.
ConnectionError
:
A
Connection error
occurred
.
DependencyWarning
:
Warned when an attempt
is
made to
import
a
module
with
mi
ssing optional
FileModeWarning
:
A
file
was opened
in
text
mode
,
but Requests determined its
binary
length
.
HTTPError
:
An HTTP error
occurred
.
ReadTimeout
:
The server did
not
send any
data
in
the allotted amount of time
.
RequestException
:
There was an ambiguous exception
that occurred
while
handl
ing your
Timeout
:
The request timed
out
.
TooManyRedirects
:
Too many
redirects
.
URLRequired
:
A
valid URL
is
required to
make
a
request
.
function
:
delete
:
Sends
a
DELETE
request
.
get
:
Sends
a
GET
request
.
head
:
Sends
a
HEAD
request
.
options
:
Sends
a
OPTIONS
request
.
patch
:
Sends
a
PATCH
request
.
post
:
Sends
a
POST
request
.
put
:
Sends
a
PUT
request
.
request
:
Constructs
and
sends
a
:
class
:
`
Request
<
Request
>
`.
session
:
Returns
a
:
class
:
`
Session
`
for
context
-
management
.
Python 玩轉網易雲音樂
pip install ncmbot
import
ncmbot
#登錄
bot
=
ncmbot
.
login
(
phone
=
"xxx"
,
password
=
"yyy"
)
bot
.
content
# bot.json()
#獲取用戶歌單
ncmbot
.
user_play_list
(
uid
=
"36554272"
)
下載視頻字幕
pip install getsub
Python 財經數據介麵包
pip install tushare
import
tushare
as
ts
#一次性獲取最近一個日交易日所有股票的交易數據
ts
.
get_today_all
()
代碼,名稱,漲跌幅,現價,開盤價,最高價,最低價,最日收盤價,成交量,換手率
code
name changepercent trade open
high low
settlement
0
002738
中礦資源
10.023
19.32
19.32
19.32
19.32
17.56
1
300410
正業科技
10.022
25.03
25.03
25.03
25.03
22.75
2
002736
國信證券
10.013
16.37
16.37
16.37
16.37
14.88
3
300412
迦南科技
10.010
31.54
31.54
31.54
31.54
28.67
4
300411
金盾股份
10.007
29.68
29.68
29.68
29.68
26.98
5
603636
南威軟體
10.006
38.15
38.15
38.15
38.15
34.68
6
002664
信質電機
10.004
30.68
29.00
30.68
28.30
27.89
7
300367
東方網力
10.004
86.76
78.00
86.76
77.87
78.87
8
601299
中國北車
10.000
11.44
11.44
11.44
11.29
10.40
9
601880
大連港
10.000
5.72
5.34
5.72
5.22
5.20
10
000856
冀東裝備
10.000
8.91
8.18
8.91
8.18
8.10
開源漏洞靶場
# 安裝pip
curl
-
s
https
://
bootstrap
.
pypa
.
io
/
get
-
pip
.
py
|
python3
# 安裝docker
apt
-
get
update
&&
apt
-
get install
docker
.
io
# 啟動docker服務
service docker
start
# 安裝compose
pip install
docker
-
compose
# 拉取項目
git clone
git
@
github
.
com
:
phith0n
/
vulhub
.
git
cd
vulhub
# 進入某一個漏洞/環境的目錄
cd
nginx_php5_mysql
# 自動化編譯環境
docker
-
compose
build
# 啟動整個環境
docker
-
compose
up
-
d
#測試完成後,刪除整個環境
docker
-
compose
down
北京實時公交
pip
install
-
r
requirements
.
txt
安裝依賴
python
manage
.
py
build
_
cache獲取離線數據,建立本地緩存
#項目自帶了一個終端中的查詢工具作為例子,運行: python manage.py cli
>>>
from
beijing_bus
import
BeijingBus
>>>
lines
=
BeijingBus
.
get_all_lines
()
>>>
lines
[
<
Line
:
運通122
(
農業展覽館-
華紡易城公交場站)
>
,
<
Line
:
運通101
(
廣順南大街北口-
藍龍家園)
>
,
...]
>>>
lines
=
BeijingBus
.
search_lines
(
"847"
)
>>>
lines
[
<
Line
:
847
(
馬甸橋西-
雷庄村)
>
,
<
Line
:
847
(
雷庄村-
馬甸橋西)
>
]
>>>
line
=
lines
[
0
]
>>>
line
.
id
,
line
.
name
541
847
(
馬甸橋西-
雷庄村)
>>>
line
.
stations
[
<
Station
馬甸橋西
>
,
<
Station
馬甸橋東
>
,
<
Station
安華橋西
>
,
...]
>>>
station
=
line
.
stations
[
0
]
>>>
station
.
name
,
station
.
lat
,
station
.
lon
馬甸橋西
39.967721
116.372921
>>>
line
.
get_realtime_data
(
1
)
# 參數為站點的序號,從1開始
[
{
"id"
:
公交車id
,
"lat"
:
公交車的位置,
"lon"
:
公交車位置,
"next_station_name"
:
下一站的名字,
"next_station_num"
:
下一站的序號,
"next_station_distance"
:
離下一站的距離,
"next_station_arriving_time"
:
預計到達下一站的時間,
"station_distance"
:
離本站的距離,
"station_arriving_time"
:
預計到達本站的時間,
},
...
]
文章提取器
git clone
https
://
github
.
com
/
grangier
/
python
-
goose
.
git
cd
python
-
goose
pip
install
-
r
requirements
.
txt
python
setup
.
py
install
>>>
from
goose
import
Goose
>>>
from
goose
.
text
import
StopWordsChinese
>>>
url
=
"http://www.bbc.co.uk/zhongwen/simp/chinese_news/2012/12/121210_hongkong_politics.shtml"
>>>
g
=
Goose
({
"stopwords_class"
:
StopWordsChinese
})
>>>
article
=
g
.
extract
(
url
=
url
)
>>>
article
.
cleaned_text
[
:
150
]
香港行政長官梁振英在各方壓力下就其大宅的違章建築(僭建)問題到立法會接受質詢,並向香港民眾道歉。
梁振英在星期二(
12
月10
日)的答問大會開始之際在其演說中道歉,但強調他在違章建築問題上沒有隱瞞的意圖和動機。
一些親北京陣營議員歡迎梁振英道歉,且認為應能獲得香港民眾接受,但這些議員也質問梁振英有
Python 藝術二維碼生成器
pip install MyQR
myqr
https
://
github
.
com
myqr
https
://
github
.
com
-
v
10
-
l
Q
偽裝瀏覽器身份
pip install
fake
-
useragent
from
fake_useragent
import
UserAgent
ua
=
UserAgent
()
ua
.
ie
# Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
ua
.
msie
# Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)"
ua
[
"Internet Explorer"
]
# 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)
ua
.
opera
# Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11
ua
.
chrome
# 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
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"
:
"https://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_{size}.jpg"
,
"badge"
:
[],
"name"
:
"我是小號"
,
"headline"
:
"程序員"
,
"gender"
: -
1
,
"user_type"
:
"people"
,
"is_advertiser"
:
False
,
"avatar_url"
:
"https://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
:
演唱會
音樂會
音樂劇
歌舞劇
兒童劇
話劇
歌劇
比賽
舞蹈
戲曲
相聲
雜技
馬戲
魔術
看完本文有收穫?請轉
發分享給更多人
關注「P
ython開發者」,提升Python技能
抓取發放代理
※幫你提升 Python 的 27 種編程語言
※Python 性能優化
※Fedora 需要幾年時間才能從 Python 2 切換到 Python 3
※圖解機器學習:神經網路和 TensorFlow 的文本分類
※使用 MPI for Python 並行化遺傳演算法
TAG:Python開發者 |
※Python帶我飛:50個有趣而又鮮為人知的Python特性
※用 Python 編寫的 Python 解釋器
※Python os模塊的使用
※windows下python常用庫的安裝
※為啥子要用ipython
※Python小技巧:QPython,一個在手機上運行Python的神器
※使用 Python 和 Prometheus 跟蹤天氣
※如何用 Python 寫 Alfred Workflow
※WTF Python:有趣且鮮為人知的Python特性
※python2和python3有什麼不同?
※對 Python 開發者而言,IPython 仍然是 Jupyter Notebook 的核心
※Python高手和Python菜鳥的區別是什麼?
※如何開發一個基於 Docker 的 Python 應用
※使用 attrs 來告別 Python 中的樣板
※python的concat等用法
※在瀏覽器中用Python做數據科學:Pyodide
※Map和Reduce在Hadoop與Python中有何異同?
※Python爬蟲利器:Requests庫的使用
※為什麼Python如此火?Why Python is so popular?
※Ubuntu下系統在帶Python和conda中安裝的Python共存問題