那些有趣/用的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 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_code
- s, 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 玩轉(zhuǎn)網(wǎng)易云音樂
- 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 財經(jīng)數(shù)據(jù)接口包
- pip install tushare
- import tushare as ts
- #一次性獲取最近一個日交易日所有股票的交易數(shù)據(jù)
- ts.get_today_all()
- 代碼,名稱,漲跌幅,現(xiàn)價,開盤價,最高價,最低價,最日收盤價,成交量,換手率
- code name changepercent trade open high low settlement \
- 0 002738 中礦資源 10.023 19.32 19.32 19.32 19.32 17.56
- 1 300410 正業(yè)科技 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 信質(zhì)電機(jī) 10.004 30.68 29.00 30.68 28.30 27.89
- 7 300367 東方網(wǎng)力 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服務(wù)
- service docker start
- # 安裝compose
- pip install docker-compose
- # 拉取項目
- git clone git@github.com:phith0n/vulhub.git
- cd vulhub
- # 進(jìn)入某一個漏洞/環(huán)境的目錄
- cd nginx_php5_mysql
- # 自動化編譯環(huán)境
- docker-compose build
- # 啟動整個環(huán)境
- docker-compose up -d
- #測試完成后,刪除整個環(huán)境
- docker-compose down
北京實時公交
- pip install -r requirements.txt 安裝依賴
- python manage.py build_cache 獲取離線數(shù)據(jù),建立本地緩存
- #項目自帶了一個終端中的查詢工具作為例子,運行: python manage.py cli
- >>> from beijing_bus import BeijingBus
- >>> lines = BeijingBus.get_all_lines()
- >>> lines
- [<Line: 運通122(農(nóng)業(yè)展覽館-華紡易城公交場站)>, <Line: 運通101(廣順南大街北口-藍(lán)龍家園)>, ...]
- >>> 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) # 參數(shù)為站點的序號,從1開始
- [
- {
- 'id': 公交車id,
- 'lat': 公交車的位置,
- 'lon': 公交車位置,
- 'next_station_name': 下一站的名字,
- 'next_station_num': 下一站的序號,
- 'next_station_distance': 離下一站的距離,
- 'next_station_arriving_time': 預(yù)計到達(dá)下一站的時間,
- 'station_distance': 離本站的距離,
- 'station_arriving_time': 預(yù)計到達(dá)本站的時間,
- },
- ...
- ]
文章提取器
- 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)
- >>> print article.cleaned_text[:150]
香港行政長官梁振英在各方壓力下就其大宅的違章建筑(僭建)問題到立法會接受質(zhì)詢,并向香港民眾道歉。
梁振英在星期二(12月10日)的答問大會開始之際在其演說中道歉,但強(qiáng)調(diào)他在違章建筑問題上沒有隱瞞的意圖和動機(jī)。
一些親北京陣營議員歡迎梁振英道歉,且認(rèn)為應(yīng)能獲得香港民眾接受,但這些議員也質(zhì)問梁振英有
Python 藝術(shù)二維碼生成器
- 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
- print ifconfig("eth0")
處理中文文本內(nèi)容
- 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'「繁體字」「繁體中文」的叫法
- # 在臺灣亦很常見。
抓取發(fā)放代理
- 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 可選參數(shù),待驗證的 proxies 列表文件
- --out-proxy 可選參數(shù),輸出已驗證的 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}
解析 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 出發(fā)站
- to 到達(dá)站
- date 查詢?nèi)掌?nbsp;
- city 查詢城市
- show 演出的類型
- days 查詢近(幾)天內(nèi)的演出, 若省略, 默認(rèn)15
- city 城市名,加在-p后查詢該城市所有莆田醫(yī)院
- hospital 醫(yī)院名,加在city后檢查該醫(yī)院是否是莆田系
- Options:
- -h, --help 顯示該幫助菜單.
- -dgktz 動車,高鐵,快速,特快,直達(dá)
- -m 熱映電影查詢
- -p 莆田系醫(yī)院查詢
- -l 歌詞查詢
- -c 彩票查詢
- Show:
- 演唱會 音樂會 音樂劇 歌舞劇 兒童劇 話劇
- 歌劇 比賽 舞蹈 戲曲 相聲 雜技 馬戲 魔術(shù)