Giter Site home page Giter Site logo

tk_lotto's Introduction

Tk_lotto

tkinter 라이브러리와 Python 언어를 사용해서 로또 회차 입력 받아 당첨 번호를 확인하는 프로그램 개발

창 생성

tkinter 모듈을 불러오고 새로운 창을 생성한다.

from tkinter import *
win = Tk()
win.geometry("300x100")
win.option_add("*Font", "궁서 20")

win.mainloop()

버튼 생성

로또 당첨 번호를 확인하는 버튼을 생성한다. command의 함수는 아직 미구현으로 둔다.

btn = Button(win)
btn.config(text="로또 당첨 번호 확인")
btn.config(command=)
btn.pack()

입력창 생성

입력창을 생성하는 함수 Entry() 안에 매개변수로 win(창)을 주고 ent 변수에 저장한 뒤 pack()함수로 입력창을 배치한다.

ent = Entry(win)
ent.pack()

입력창에 입력한 값을 받는 get()함수를 사용해서 입력값을 출력하는 ent_p 함수를 생성한 후 a에 저장하고
버튼의 command 옵션으로 이 ent_p 함수를 전달한다.

from tkinter import *
win = Tk()
win.geometry("300x100")
win.option_add("*Font", "궁서 20")

ent = Entry(win)
ent.pack()


def ent_p():
    a = ent.get()
    print(a)

btn = Button(win)
btn.config(text="로또 당첨 번호 확인")
btn.config(command=ent_p)
btn.pack()
win.mainloop()

회차별 당첨번호 웹사이트에서 크롤링하기

requests 모듈을 import 해준다.

import requests

url 변수를 선언하고 값으로 크롤링할 웹사이트의 주소를 넣어준 뒤 requests 모듈의 get()함수 안에 url 변수를 넣어주고 이 값을 req 변수에 저장한다.
그 후 req.text를 찍어보면 url 값에 저장된 웹페이지의 html 문서를 불러오는 것을 알 수 있다.

url = "https://dhlottery.co.kr/gameResult.do?method=byWin&drwNo=999"
req = requests.get(url)
req.text

하지만 이 내용이 복잡하므로 beautifulsoup4를 다운해주고 이 툴을 사용한다.

pip install beautifulsoup4

이 모듈을 사용하기 위해 import를 해주고 이전의 req.text를 BeautifulSoup의 매개변수로 넣어주고 사용하는 방식으로 "html.parser"를 두 번째 매개 변수에 넣어준다. 이것을 soup 변수에 넣고 출력해보면
그냥 req.text를 출력할 때보다 더 깔끔하게 출력이 된다.

from bs4 import BeautifulSoup
soup = BeautifulSoup(req.text, "html.parser")

그리고 웹페이지에서 필요한 div와 class의 내용을 크롤링하기 위해 find() 함수를 이용해서 첫 번째 매개 변수로 "div"를 두 번째 매개변수로 딕셔너리 형태의 attrs = {"class","win_result"}를 넣어준다.

soup.find("div", attrs = {"class","win_result"})

그 뒤에 .get_text() 함수를 사용해서 텍스트만을 뽑아주고 코드가 길어지니 이 코드를 txt 변수에 저장한다.

txt = soup.find("div", attrs = {"class","win_result"}).get_text()

그 후 txt에 split()함수로 \n 띄어쓰기 기준으로 끊고 리스트의 7번째에서 13번째까지 인덱싱을 해주면 필요한 로또번호 숫자가 나오는데 이를 num_list에 저장한다.

num_list = txt.split("\n")[7:13]

보너스 번호 역시 인덱싱을 해주면 txt.split("\n")[-4]가 되고 이를 bonus에 저장한다.

bonus = txt.split("\n")[-4]

지금까지 구한 값들을 출력해준다.

print("당첨번호")
print(num_list)
print("보너스번호")
print(bonus)

위에서 크롤링해올때 drwNo의 값에 오는 숫자가 회차가 되므로 이 회차를 우리가 입력창에 입력한 값으로 지정해준다.

n = ent.get()
url = "https://dhlottery.co.kr/gameResult.do?method=byWin&drwNo={}".format(n)
req = requests.get(url)
req.text

최종 코드와 프로그램 실행 결과

최종 코드는 다음과 같습니다.

from tkinter import *

win = Tk()
win.geometry("1000x500")
win.option_add("*Font", "궁서 15")

ent = Entry(win)
ent.pack()


def lotto_p():
    import requests
    from bs4 import BeautifulSoup
    n = ent.get()
    url = "https://dhlottery.co.kr/gameResult.do?method=byWin&drwNo={}".format(
        n)
    req = requests.get(url)
    soup = BeautifulSoup(req.text, "html.parser")
    txt = soup.find("div", attrs={"class", "win_result"}).get_text()
    num_list = txt.split("\n")[7:13]
    bonus = txt.split("\n")[-4]
    btn.config(text="{}회차의 로또 당첨 번호는 {}이고 보너스 번호는 {}입니다.".format(
        n, num_list, bonus))


btn = Button(win)
btn.config(text="로또 당첨 번호 확인")
btn.config(command=lotto_p)
btn.pack()
win.mainloop()

프로그램을 실행하면 다음과 같습니다. lotto

입력창에 예로 999를 입력하고 로또 당첨 번호 확인 버튼을 누르면 999회차의 로또번호를 보너스번호와 함께 버튼에 출력해줍니다. lotto1

tk_lotto's People

Contributors

chihyeonwon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.