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

    你可能感兴趣的文章
    Nacos做注册中心使用
    查看>>
    Nacos原理
    查看>>
    Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
    查看>>
    Nacos启动异常
    查看>>
    Nacos和Zookeeper对比
    查看>>
    Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
    查看>>
    Nacos如何实现Raft算法与Raft协议原理详解
    查看>>
    Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(下)
    查看>>
    Nacos心跳机制实现快速上下线
    查看>>
    Nacos服务注册与发现demo
    查看>>
    Nacos服务注册总流程(源码分析)
    查看>>
    nacos服务注册流程
    查看>>
    Nacos服务部署安装
    查看>>
    nacos本地可以,上服务器报错
    查看>>
    Nacos注册中心有几种调用方式?
    查看>>
    nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
    查看>>
    nacos源码 nacos注册中心1.4.x 源码 nacos源码如何下载 nacos 客户端源码下载地址 nacos discovery下载地址(一)
    查看>>
    Nacos简介、下载与配置持久化到Mysql
    查看>>
    Nacos简介和控制台服务安装
    查看>>