歡迎光臨
每天分享高質量文章

玩這麼久 Python ,這些好玩又實用的庫一定不能錯過!

圖片處理

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 youtubedl #直接安裝youtube-dl

pip installU youtubedl #安裝youtube-dl並更新

youtubedl “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_code

s, structures, utils, warnings

special attribute:

    __doc__

class:

    NullHandler: This handler does nothing. Its intended to be used to avoid th

e

    PreparedRequest: The fully mutable :class:`PreparedRequest <PreparedRequest>

` object,

    Request: A usercreated :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 contextmanagement.

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

curls https://bootstrap.pypa.io/getpip.py | python3

 

# 安裝docker

aptget update && aptget install docker.io

 

# 啟動docker服務

service docker start

 

# 安裝compose

pip install dockercompose

# 拉取專案

git clone git@github.com:phith0n/vulhub.git

cd vulhub

 

# 進入某一個漏洞/環境的目錄

cd nginx_php5_mysql

 

# 自動化編譯環境

dockercompose build

 

# 啟動整個環境

dockercompose upd

#測試完成後,刪除整個環境

dockercompose down

北京實時公交

pip installr 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]

>>> print line.id, line.name

541 847(馬甸橋西雷莊村)

>>> line.stations

[<Station 馬甸橋西>, <Station 馬甸橋東>, <Station 安華橋西>, …]

>>> station = line.stations[0]

>>> print 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/pythongoose.git

cd pythongoose

pip installr 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)

>>> print article.cleaned_text[:150]

香港行政長官梁振英在各方壓力下就其大宅的違章建築(僭建)問題到立法會接受質詢,並向香港民眾道歉。

 

梁振英在星期二(1210日)的答問大會開始之際在其演說中道歉,但強調他在違章建築問題上沒有隱瞞的意圖和動機。

 

一些親北京陣營議員歡迎梁振英道歉,且認為應能獲得香港民眾接受,但這些議員也質問梁振英有

Python 藝術二維碼生成器

pip  install  MyQR

myqr https://github.com

myqr https://github.comv 10l Q

偽裝瀏覽器身份

pip install fakeuseragent

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

print ifconfig(“eth0”)

處理中文文字內容

pip installU 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 installU getproxy

~ getproxyhelp

Usage: getproxy [OPTIONS]

 

Options:

inproxy TEXT Input proxy file

outproxy 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/zhihuapiupgrade

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|電影)

        iqueryp <city>

        iqueryl song [singer]

        iqueryp <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:

        演唱會 音樂會 音樂劇 歌舞劇 兒童劇 話劇

        歌劇 比賽 舞蹈 戲曲 相聲 雜技 馬戲 魔術

來源:蘇生不惑

segmentfault.com/a/1190000010103386



————近期開班————

馬哥聯合BAT、豆瓣等一線網際網路Python開發達人,根據目前企業需求的Python開發人才進行了深度定製,加入了大量一線網際網路公司:大眾點評、餓了麼、騰訊等生產環境真是專案,課程由淺入深,從Python基礎到Python高階,讓你融匯貫通Python基礎理論,手把手教學讓你具備Python自動化開發需要的前端介面開發、Web框架、大監控系統、CMDB系統、認證堡壘機、自動化流程平臺六大實戰能力,讓你從0開始蛻變成Hold住年薪20萬的Python自動化開發人才

10期面授班:2018年03月05號(北京)

11期網路班:2018年03月17號網路

掃描二維碼領取學習資料

更多Python好文請點選【閱讀原文】哦

↓↓↓

贊(0)

分享創造快樂

© 2024 知識星球   網站地圖