博客
关于我
元宵节在家,我用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/

    你可能感兴趣的文章
    Objective-C实现PrimeFactors质因子分解算法 (附完整源码)
    查看>>
    Objective-C实现pythagoras哥拉斯算法(附完整源码)
    查看>>
    Objective-C实现qubit measure量子位测量算法(附完整源码)
    查看>>
    Objective-C实现quick select快速选择算法(附完整源码)
    查看>>
    Objective-C实现radians弧度制算法(附完整源码)
    查看>>
    Objective-C实现radix sort基数排序算法(附完整源码)
    查看>>
    Objective-C实现rayleigh quotient瑞利商算法(附完整源码)
    查看>>
    Objective-C实现RC4加解密算法(附完整源码)
    查看>>
    Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
    查看>>
    Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
    查看>>
    Objective-C实现RedBlackTree红黑树算法(附完整源码)
    查看>>
    Objective-C实现redis分布式锁(附完整源码)
    查看>>
    Objective-C实现reverse letters反向字母算法(附完整源码)
    查看>>
    Objective-C实现ripple adder涟波加法器算法(附完整源码)
    查看>>
    Objective-C实现RodCutting棒材切割最大利润算法(附完整源码)
    查看>>
    Objective-C实现Romberg算法(附完整源码)
    查看>>
    Objective-C实现round robin循环赛算法(附完整源码)
    查看>>
    Objective-C实现RRT路径搜索(附完整源码)
    查看>>
    Objective-C实现rsa 密钥生成器算法(附完整源码)
    查看>>
    Objective-C实现RSA密码算法(附完整源码)
    查看>>