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

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

??????????

???????????

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

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

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

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

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

  • ?????????

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

  • 2.2 ????

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

    import requests
    from bs4 import BeautifulSoup
    import 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 showinfo
    import 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 dist
        python -O main.py

        6. ????

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

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

    你可能感兴趣的文章
    mysql_secure_installation初始化数据库报Access denied
    查看>>
    MySQL_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>
    MySQL、HBase 和 Elasticsearch:特点与区别详解
    查看>>
    MySQL、Redis高频面试题汇总
    查看>>
    MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
    查看>>
    mysql一个字段为空时使用另一个字段排序
    查看>>
    MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
    查看>>
    MYSQL一直显示正在启动
    查看>>
    MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
    查看>>
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>
    MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
    查看>>
    MySQL不同字符集及排序规则详解:业务场景下的最佳选
    查看>>
    Mysql不同官方版本对比
    查看>>
    MySQL与Informix数据库中的同义表创建:深入解析与比较
    查看>>
    mysql与mem_细说 MySQL 之 MEM_ROOT
    查看>>
    MySQL与Oracle的数据迁移注意事项,另附转换工具链接
    查看>>
    mysql丢失更新问题
    查看>>