python|用Python写的猜灯谜软件源代码

主要实现原理:首先通过爬虫获取灯谜的数据,灯谜数据来源于汉谜网,然后用保存为csv或者表格数据,并用用tk做界面进行展示。完整程序代码包请在文末地址下载,程序运行截图:
python|用Python写的猜灯谜软件源代码
文章图片

从汉谜网爬取灯谜程序spider.py

# -*- coding:utf-8 -*- # 更多Python源代码,请微信关注:Python代码大全 import requests from lxml import etree import pandas as pd from bs4 import BeautifulSoup as bsclass Riddles(object): def __init__(self): self.df = pd.DataFrame(columns=['谜面', '谜底','提示'])def get_list(self, url): r = requests.get(url) self.parse_list(r.text)def parse_list(self, html): s = etree.HTML(html) items = s.xpath('/html/body/div[6]/div[1]/div/div[2]/ul/li') for item in items: detUrl = r'http://www.cmiyu.com' + item.xpath('a/@href')[0] self.get_detail(detUrl) self.save_data()def get_detail(self, url): print(url) r = requests.get(url) r.encoding = 'gb2312' soup = bs(r.text, 'lxml') prompt = soup.find('div', class_='zy').text h3s = soup.find('div', class_='md').find_all('h3') ques = h3s[0].text ans = h3s[1].text self.df = self.df.append(pd.DataFrame.from_dict({'谜面': ques, '谜底': ans, '提示': prompt}, orient='index').T, ignore_index=True)def save_data(self): self.df.to_csv('new_data1.csv', index=False, mode='a') self.df = pd.DataFrame(columns=['谜面', '谜底', '提示'])if __name__ == '__main__': riddles = Riddles() for i in range(20, 151): print('page', str(i)) url = r'http://www.cmiyu.com/dmmy/my18' + str(i) + '.html' print(url) riddles.get_list(url)

主程序main.py
# 更多Python源代码,请微信关注:Python代码大全from tkinter import messagebox from PIL import Image, ImageTk import random import csv import tkinter as tkclass LanternRiddles(object): def __init__(self): self.root = tk.Tk() self.root.title("猜灯谜软件-《Python代码大全》") self.root.geometry("1200x500") self.root.geometry("+100+150") self.data = https://www.it610.com/article/[] with open('new_data.csv', 'r') as f: reader = csv.reader(f) for row in reader: self.data.append(row) self.index = [i for i in range(len(self.data))] random.shuffle(self.index)# 做成背景的装饰 pic1 = Image.open('pic/bg.jpg').resize((1200, 500))# 加载图片并调整大小至窗口大小 pic = ImageTk.PhotoImage(pic1) render = tk.Label(self.root, image=pic, compound=tk.CENTER, justify=tk.LEFT) render.place(x=0, y=0)# 标签 and 输入框 label = tk.Label(self.root, text='输入答案', font=('微软雅黑', 15), fg='black', bg="Magenta") label.place(x=0, y=10, width=100, height=40) self.entry = tk.Entry(self.root, font=('宋体', 15), width=15, bg="GhostWhite") self.entry.place(x=110, y=10, width=150, height=40)# 设置输入框,输入答案 # 按钮 confirm_button = tk.Button(self.root, text='确认', font=('微软雅黑', 15), bg="LightGreen", command=self.check) confirm_button.place(x=270, y=10, width=100, height=40)# 确定按钮quit_button = tk.Button(self.root, text='退出软件', font=('微软雅黑', 15), bg="LightGreen", command=self.quit) quit_button.place(x=800, y=10, width=100, height=40)# 退出软件 start_button = tk.Button(self.root, text='开始答题', font=('微软雅黑', 15), bg="LightGreen", command=self.get_next) start_button.place(x=0, y=80, width=100, height=40)# 更换题目 prompt_button = tk.Button(self.root, text='显示提示', font=('微软雅黑', 15), bg="LightGreen", command=self.show_prompt) prompt_button.place(x=650, y=10, width=100, height=40)# 更换题目self.riddle = tk.Text(self.root, bg="OrangeRed", fg="dimgray",font=('微软雅黑', 15)) self.riddle.place(x=200, y=180, width=300, height=160)# 显示题目self.root.mainloop()def get_next(self):# 更换题目 self.riddle.delete('1.0', 'end')# 清空显示 index = random.choice(self.index) self.index.remove(index) self.question = self.data[index][0] self.answer = self.data[index][1] self.prompt = self.data[index][2] self.riddle.insert(tk.END, self.question)def check(self):# 验证答案 reply = self.entry.get() if reply in self.answer: messagebox.showinfo('提示', '回答正确') self.get_next() self.entry.delete(0, tk.END) else: messagebox.showinfo('提示', '回答错误,请重试') self.entry.delete(0, tk.END)def show_prompt(self):# 显示提示 messagebox.showinfo('提示', self.prompt)def quit(self): self.root.destroy()if __name__ == '__main__': LanternRiddles()

完整猜灯谜软件源代码:猜灯谜软件
【python|用Python写的猜灯谜软件源代码】更多Python源代码免费下载,请微信关注:Python代码大全
python|用Python写的猜灯谜软件源代码
文章图片

    推荐阅读