博客
关于我
元宵节在家,我用Python撸一个猜灯谜
阅读量:152 次
发布时间:2019-02-27

本文共 3251 字,大约阅读时间需要 10 分钟。

??????????

???????????

?????????????????????????????????????????????????????????????????????????????????????????????

2. ??????????????

???????????????????????????????????????????????????????????????????????????????????????

2.1 ??????
  • ?????????

    ??????????????????????????????????

  • ?????????

    ??????????????????????????????????????????

  • 2.2 ????

    ??Python?????????requests????????BeautifulSoup?????????????????????????

    import requestsfrom bs4 import BeautifulSoupimport csv# ??????def get_chinese_riddles():    # ?????????    riddles_links = []    soup = BeautifulSoup(requests.get('https://example.com/hompage.html'), 'html.parser')    for link in soup.find_all('a', {'target': '_blank'}):        riddles_links.append(link.get('href'))        # ???????????    riddle_data = []    for link in riddles_links:        soup = BeautifulSoup(requests.get(link, 'html.parser'), 'html.parser')        for riddle in soup.find_all('div', {'class': 'riddle-container'}):            try:                prompt = riddle.find('p', {'class': 'prompt'}).text.strip()                question = riddle.find('p', {'class': 'question'}).text.strip()                answer = riddle.find('p', {'class': 'answer'}).text.strip()                riddle_data.append({                    'prompt': prompt,                    'question': question,                    'answer': answer                })            except:                pass    return riddle_data# ???if __name__ == '__main__':    riddles = get_chinese_riddles()    # ???csv??    with open('riddles.csv', 'w') as f:        csv.writer(f).writerow(riddles)

    3. ?????????????

    ????????????????????pandas???????DataFrame??????????

    3.1 ????
    import pandas as pd# ??csv??data = pd.read_csv('riddles.csv')# ????print(data.head())
    3.2 ?????

    ????????????????????????JSON???

    data.to_json('riddles.json')

    4. ?????????

    ??tkinter?????????????????????????????????????

    4.1 ????
    from tkinter import *from tkinter.messagebox import showinfoimport random# ?????root = Tk()root.title('??????')root.geometry('600x400')# ????def get_riddle():    global riddle_index    riddle_index = random.randint(0, len(riddles_list))    riddle_label.config(text=riddles_list[riddle_index]['prompt'])def check_answer(entry):    global riddle_index    if entry.strip() == riddles_list[riddle_index]['answer']:        showinfo('??', '???')    else:        showinfo('??', '???')def show_tip():    showinfo('??', riddles_list[riddle_index]['question'])# ???riddles_list = []try:    data = pd.read_json('riddles.json')    riddles_list = data.to_dict('records')except:    print('??????')# ????riddle_label = Label(root, text='??????', font=('Arial', 20, 'bold'))riddle_label.pack(pady=20)entry = Entry(root)entry.pack(pady=10)button_change = Button(root, text='????', command=get_riddle)button_change.pack(pady=10)button_check = Button(root, text='????', command=lambda: check_answer(entry))button_check.pack(pady=10)button_tip = Button(root, text='????', command=show_tip)button_tip.pack(pady=10)# ???root.mainloop()

    5. ??????????????

    ??pyinstaller?Python????????????????????

    5.1 ????
  • ??pyinstaller?
  • pip install pyinstaller
    1. ?????
    2. pyinstaller main.py
      1. ????????
      2. cd distpython -O main.py

        6. ????

        ???????????????????????????????????????????????????????????????????????

    转载地址:http://iwpd.baihongyu.com/

    你可能感兴趣的文章
    MySQL:什么样的字段适合加索引?什么样的字段不适合加索引
    查看>>
    MySQL:判断逗号分隔的字符串中是否包含某个字符串
    查看>>
    MySQL:某个ip连接mysql失败次数过多,导致ip锁定
    查看>>
    MySQL:索引失效场景总结
    查看>>
    Mysql:避免重复的插入数据方法汇总
    查看>>
    MyS中的IF
    查看>>
    M_Map工具箱简介及地理图形绘制
    查看>>
    m_Orchestrate learning system---二十二、html代码如何变的容易
    查看>>
    M×N 形状 numpy.ndarray 的滑动窗口
    查看>>
    m个苹果放入n个盘子问题
    查看>>
    n = 3 , while n , continue
    查看>>
    n 叉树后序遍历转换为链表问题的深入探讨
    查看>>
    N!
    查看>>
    N-Gram的基本原理
    查看>>
    n1 c语言程序,全国青少年软件编程等级考试C语言经典程序题10道七
    查看>>
    Nacos Client常用配置
    查看>>
    nacos config
    查看>>
    Nacos Config--服务配置
    查看>>
    Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
    查看>>
    Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
    查看>>