Create an English word app with python

specification

--Scraping the entered word from the translation site and displaying the translation result --Outputs audio files at the same time as translation --Save the audio file with the word name .mp3

Library

The libraries used are as follows.

Library Use
os Check the path
requests Get html, download mp3
bs4 html analysis
tkinter Creating a GUI
pygame Play mp3
random Exception handling

code

import os
import random
import tkinter
import requests
from bs4 import BeautifulSoup
from pygame import mixer


class Translation:
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('Translation')
        self.root.geometry('500x200')
        self.root.attributes('-topmost', True)
        self.text_box = tkinter.Entry(width=20, font=('', 20), justify='center')
        self.text_box.focus_set()
        self.text_box.pack()
        self.root.bind('<Return>', self.scraping)
        self.root.bind('<space>', self.delete)
        self.answer = tkinter.Message(self.root, text='', font=('', 20), width=450)
        self.answer.pack(anchor='center', expand=1)
        self.root.mainloop()
    
    def scraping(self, event): #Scraping
        try:
            res = requests.get('https://ejje.weblio.jp/content/' + self.text_box.get())
            soup = BeautifulSoup(res.content, 'html.parser')
            txt = soup.find('td', {'class': 'content-explanation ej'})
            self.answer['text'] = txt.text
            self.sound(soup)
        except:
            self.answer['text'] = random.choice(('(^^;)?', '(・ Ω ・)?', "('Д')?", '(;・`д ・ ´)?'))
    
    def sound(self, soup): #Download and play mp3
        mp3_directory_path = 'd:/python/Application/mp3/' #mp3 save destination
        if not os.path.exists(mp3_directory_path):
            os.mkdir(mp3_directory_path)

        if os.path.exists(mp3_directory_path + self.text_box.get() + '.mp3'):
            pass
        else:
            audio = soup.find('audio', {'class': 'contentAudio'})
            src = audio.find('source')['src']
            res = requests.get(src, stream=True)

            with open(mp3_directory_path + self.text_box.get() + '.mp3', 'wb') as f:
                f.write(res.content)

        mixer.init()
        mixer.music.load(mp3_directory_path + self.text_box.get() + '.mp3')
        mixer.music.play()

    def delete(self, event): #Delete all characters in the input field with the space key
        self.text_box.delete(0, tkinter.END)


Translation()

Execution result

result1.PNG

result2.PNG

Impressions

I managed to get it into shape. The Qiita article made by another person was very helpful. After that, if you create a bat file on your desktop, you can easily launch it. You may also be able to create word listening tests from downloaded mp3 files.

Recommended Posts

Create an English word app with python
Create an app that guesses students with python
Quickly create an excel file with Python #python
Create a word frequency counter with Python 3.4
[Python] Quickly create an API with Flask
Create an image composition app with Flask + Pillow
Create an image with characters in python (Japanese)
Create an API server quickly with Python + Falcon
Create an animated GIF local server with Python + Flask
Create an environment with virtualenv
Create an API with Django
Create 3d gif with python3
Creating an egg with python
Create an app that guesses students with python-GUI version
Create a directory with python
Create an LCD (16x2) game with Raspberry Pi and Python
Let's create an app that authenticates with OIDC with Azure AD
How to run an app built with Python + py2app built with Anaconda
Create a Todo app with Django ① Build an environment with Docker
[Python Kivy] How to create an exe file with pyinstaller
Cut out an image with python
Create plot animation with Python + Matplotlib
Create folders from '01' to '12' with python
Create a virtual environment with Python!
Create an age group with pandas
Draw an illustration with Python + OpenCV
[Python] Send an email with outlook
How to create a heatmap with an arbitrary domain in Python
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part ③
Create an application by classifying with Pygame
[Python] Building an environment with Anaconda [Mac]
Create a Python function decorator with Class
Automatically create Python API documentation with Sphinx
Creating an image splitting app with Tkinter
Create wordcloud from your tweet with python3
Build a blockchain with Python ① Create a class
Create an image processing viewer with PySimpleGUI
Create a dummy image with Python + PIL.
Uncle SES modernizes VBA app with Python
Note when creating an environment with python
Create a simple GUI app in Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Shared screen screenshot exe app with python
I tried sending an email with python.
Daemonize a Python web app with Supervisor
Create Python + uWSGI + Nginx environment with Docker
Create and decrypt Caesar cipher with python
3. Natural language processing with Python 1-1. Word N-gram
Create an update screen with Django Updateview
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create a simple web app with flask
Easy web app with Python + Flask + Heroku
Create Excel file with Python + similarity matrix
Create your first app with Django startproject
Scraping from an authenticated site with python
Create an add-in-enabled Excel instance with xlwings
Text mining with Python ② Visualization with Word Cloud
[Automation] Read a Word document with Python
Send an email with Amazon SES + Python