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

    你可能感兴趣的文章
    MySQL5.7.18主从复制搭建(一主一从)
    查看>>
    MySQL5.7.19-win64安装启动
    查看>>
    mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
    查看>>
    MySQL5.7.37windows解压版的安装使用
    查看>>
    mysql5.7免费下载地址
    查看>>
    mysql5.7命令总结
    查看>>
    mysql5.7安装
    查看>>
    mysql5.7性能调优my.ini
    查看>>
    MySQL5.7新增Performance Schema表
    查看>>
    Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
    查看>>
    Webpack 之 basic chunk graph
    查看>>
    Mysql5.7版本单机版my.cnf配置文件
    查看>>
    mysql5.7的安装和Navicat的安装
    查看>>
    mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
    查看>>
    Mysql8 数据库安装及主从配置 | Spring Cloud 2
    查看>>
    mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
    查看>>
    MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
    查看>>
    MYSQL8.0以上忘记root密码
    查看>>
    Mysql8.0以上重置初始密码的方法
    查看>>
    mysql8.0新特性-自增变量的持久化
    查看>>