Fundamentals 13 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Python Cheat Sheet: Image Processing, API Calls, Data Mining, and Automation Scripts

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 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')

Download videos with youtube-dl

Install youtube-dl and download a video from YouTube.

pip install youtube-dl
pip install -U youtube-dl
youtube-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 pdir2
import pdir, requests
pdir.pdir(requests)

NetEase Cloud Music API

Install ncmbot, log in, and fetch a user's playlist.

pip install ncmbot
import ncmbot
bot = ncmbot.login(phone='xxx', password='yyy')
bot.user_play_list(uid='36554272')

Subtitle downloader

Install getsub to download video subtitles.

pip install getsub

Financial data with Tushare

Install tushare and retrieve daily market data for all stocks.

pip install tushare
import tushare as ts
ts.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 | python3
apt-get update && apt-get install -y docker.io
service docker start
pip install docker-compose
git clone https://github.com/phith0n/vulhub.git
cd vulhub && cd nginx_php5_mysql && docker-compose build && docker-compose up -d

Beijing bus real‑time API

Use the beijing_bus package to list lines, search a line, and get real‑time data.

pip install r
from beijing_bus import BeijingBus
lines = 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.git
cd python-goose && pip install -r requirements.txt
python setup.py install
from goose import Goose
g = 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 textblob
from textblob import TextBlob
blob = TextBlob("I am happy today. I feel sad today.")
print(blob.sentiment)
pip install snownlp
from snownlp import SnowNLP
s = SnowNLP('这个东西真心很赞')
print(s.sentiments)

Fake user‑agent generation

Install fake_useragent and retrieve random browser strings.

pip install fake-useragent
from fake_useragent import UserAgent
ua = UserAgent()
print(ua.ie)

HTTP request statistics

Install httpstat to visualize curl performance.

pip install httpstat
httpstat http://httpbin.org/get

Shell commands from Python

Install sh and run ifconfig.

pip install sh
from sh import ifconfig
print(ifconfig("eth0"))

Chinese text processing

Install snownlp and TextBlob for sentiment, word segmentation, and pinyin.

pip install snownlp
from snownlp import SnowNLP
s = 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 leakPasswd
import leakPasswd
leakPasswd.findBreach('taobao')

Nginx log analysis

Install ngxtop to parse access logs and view summary statistics.

pip install ngxtop
ngxtop

Train ticket query

Install iquery and use the -dgktz option to query high‑speed train tickets.

pip install iquery
iquery -dgktz <from> <to> <date>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Sentiment Analysisdata-processingweb-scraping
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.