Python Cheat Sheet: Image Processing, API Calls, Data Mining, and Automation Scripts
This guide compiles essential Python snippets for tasks such as image manipulation with Pillow, video downloading via youtube‑dl, inspecting request objects, accessing NetEase Cloud Music, retrieving financial data with Tushare, running Docker‑based vulnerability labs, querying Beijing bus real‑time info, extracting articles with Goose, performing sentiment analysis, generating fake user agents, and more.
Image processing with Pillow
Install Pillow and use it to open an image, convert it to a NumPy array, modify pixel values, and save a new image.
pip install pillowfrom PIL import Imageimport numpy as npa = np.array(Image.open('test.jpg'))b = [255,255,255] - aim = Image.fromarray(b.astype('uint8'))im.save('new.jpg')Download videos with youtube-dl
Install youtube-dl and download a video from YouTube.
pip install youtube-dlpip install -U youtube-dlyoutube-dl "http://www.youtube.com/watch?v=-wNyEUrxzFU"Inspect request object attributes
Install pdir2 to list all attributes and methods of a requests object.
pip install pdir2import pdir, requestspdir.pdir(requests)NetEase Cloud Music API
Install ncmbot, log in, and fetch a user's playlist.
pip install ncmbotimport ncmbotbot = ncmbot.login(phone='xxx', password='yyy')bot.user_play_list(uid='36554272')Subtitle downloader
Install getsub to download video subtitles.
pip install getsubFinancial data with Tushare
Install tushare and retrieve daily market data for all stocks.
pip install tushareimport tushare as tsts.get_today_all()Vulnerability lab with Docker
Install Docker, docker-compose, clone the vulhub repository, and start a vulnerable service.
curl -s https://bootstrap.pypa.io/get-pip.py | python3apt-get update && apt-get install -y docker.ioservice docker startpip install docker-composegit clone https://github.com/phith0n/vulhub.gitcd vulhub && cd nginx_php5_mysql && docker-compose build && docker-compose up -dBeijing bus real‑time API
Use the beijing_bus package to list lines, search a line, and get real‑time data.
pip install rfrom beijing_bus import BeijingBuslines = BeijingBus.get_all_lines()line = lines[0]print(line.id, line.name)data = line.get_realtime_data(1)Article extraction with Goose
Clone Goose, install requirements, and extract cleaned text from a URL.
git clone https://github.com/grangier/python-goose.gitcd python-goose && pip install -r requirements.txtpython setup.py installfrom goose import Gooseg = Goose({'stopwords_class': StopWordsChinese})article = g.extract(url='http://www.bbc.co.uk/...')print(article.cleaned_text[:150])Sentiment analysis
Use TextBlob for English and SnowNLP for Chinese sentiment.
pip install textblobfrom textblob import TextBlobblob = TextBlob("I am happy today. I feel sad today.")print(blob.sentiment)pip install snownlpfrom snownlp import SnowNLPs = SnowNLP('这个东西真心很赞')print(s.sentiments)Fake user‑agent generation
Install fake_useragent and retrieve random browser strings.
pip install fake-useragentfrom fake_useragent import UserAgentua = UserAgent()print(ua.ie)HTTP request statistics
Install httpstat to visualize curl performance.
pip install httpstathttpstat http://httpbin.org/getShell commands from Python
Install sh and run ifconfig.
pip install shfrom sh import ifconfigprint(ifconfig("eth0"))Chinese text processing
Install snownlp and TextBlob for sentiment, word segmentation, and pinyin.
pip install snownlpfrom snownlp import SnowNLPs = SnowNLP('这个东西真心很赞')print(s.words)print(s.tags)print(s.pinyin)Password breach lookup
Install leakPasswd and check if a password appears in known breaches.
pip install leakPasswdimport leakPasswdleakPasswd.findBreach('taobao')Nginx log analysis
Install ngxtop to parse access logs and view summary statistics.
pip install ngxtopngxtopTrain ticket query
Install iquery and use the -dgktz option to query high‑speed train tickets.
pip install iqueryiquery -dgktz <from> <to> <date>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
