Python Mini Projects: Web Scraping, Chatbots, Poetry Author Classification, Lottery Generator, Auto Apology, Screen Capture, and GIF Creation
This article presents a collection of seven practical Python scripts—including a Zhihu image scraper, two interacting chatbots, a Naive Bayes poetry author classifier, a 35‑choose‑7 lottery generator, an automatic apology writer, a screen‑capture tool, and a GIF maker—each demonstrated with complete, runnable code.
Python developers are reminded not to reinvent the wheel, yet many face three common issues: difficulty discovering existing libraries, duplicating effort for simple tasks, and lacking direction after gathering data.
The article shares seven ready‑to‑run Python 3.6.4 examples that address these problems.
① Grab Zhihu Images with 30 Lines of Code
from selenium import webdriver
import time
import urllib.request
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.zhihu.com/question/29134042")
i = 0
while i < 10:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
try:
driver.find_element_by_css_selector('button.QuestionMainAction').click()
print("page" + str(i))
time.sleep(1)
except:
break
result_raw = driver.page_source
content_list = re.findall("img src=\"(.+?)\" ", str(result_raw))
n = 0
while n < len(content_list):
i = time.time()
local = (r"%s.jpg" % (i))
urllib.request.urlretrieve(content_list[n], local)
print("编号:" + str(i))
n = n + 1② Let Two Chatbots Talk to Each Other
from time import sleep
import requests
s = input("请主人输入话题:")
while True:
resp = requests.post("http://www.tuling123.com/openapi/api", data={"key":"4fede3c4384846b9a7d0456a5e1e2943", "info": s, })
resp = resp.json()
sleep(1)
print('小鱼:', resp['text'])
s = resp['text']
resp = requests.get("http://api.qingyunke.com/api.php", {'key':'free','appid':0,'msg': s})
resp.encoding = 'utf8'
resp = resp.json()
sleep(1)
print('菲菲:', resp['content'])
# Additional example using 小i robot omitted for brevity③ AI Analysis of Tang Poetry Author (Li Bai vs. Du Fu)
import jieba
from nltk.classify import NaiveBayesClassifier
# Load Li Bai and Du Fu corpora, segment with jieba, build feature sets
# Train Naive Bayes classifier
# Prompt user for a poem line, classify each word, and compute probabilities
print('李白的可能性:%.2f%%' % (x * 100))
print('杜甫的可能性:%.2f%%' % (y * 100))④ Lottery Number Generator (35 choose 7)
import random
temp = [i + 1 for i in range(35)]
random.shuffle(temp)
i = 0
list = []
while i < 7:
list.append(temp[i])
i = i + 1
list.sort()
print('\033[0;31;;1m')
print(*list[0:6], end="")
print('\033[0;34;;1m', end=" ")
print(list[-1])⑤ Automatic Apology Letter Generator
import random
import xlrd
ExcelFile = xlrd.open_workbook(r'test.xlsx')
sheet = ExcelFile.sheet_by_name('Sheet1')
i = []
x = input("请输入具体事件:")
y = int(input("老师要求的字数:"))
while len(str(i)) < y * 1.2:
s = random.randint(1, 60)
rows = sheet.row_values(s)
i.append(*rows)
print(" "*8+"检讨书"+"
"+"老师:")
print("我不应该" + str(x)+",", *i)
print("再次请老师原谅!")⑥ Screen Capture Tool
from time import sleep
from PIL import ImageGrab
m = int(input("请输入想抓屏几分钟:"))
m = m * 60
n = 1
while n < m:
sleep(0.02)
im = ImageGrab.grab()
local = (r"%s.jpg" % (n))
im.save(local, 'jpeg')
n = n + 1⑦ GIF Animation Maker
from PIL import Image
im = Image.open("1.jpg")
images = []
images.append(Image.open('2.jpg'))
images.append(Image.open('3.jpg'))
im.save('gif.gif', save_all=True, append_images=images, loop=1, duration=1, comment=b"aaabb")Each snippet is fully functional and can be executed directly, offering readers quick ways to automate web scraping, build simple conversational agents, perform basic natural‑language classification, generate random numbers, compose text automatically, capture screen frames, and create animated GIFs.
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.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.
