크롤링 - 로또 번호
본문 바로가기
파이썬(Python)/크롤링 연습

크롤링 - 로또 번호

by 뚱구리 2023. 5. 25.

심심해서 해보는 😅

역대 로또 역대 당첨 번호 알아봐서

어떤 번호가 제일 많이 나왔는지

찾아보자


참고한 사이트 너무 감사합니다 ㅋㅋ
[참고 블로그 1]
https://teddylee777.github.io/python/lotto

로또 1회부터 최신회차까지 크롤링한 뒤 파일 저장하기(requests, beautifulsoup4)

로또 1회부터 최신회차까지 크롤링한 뒤 파일 저장(requests, beautifulsoup4) 방법에 대해 알아보겠습니다.

teddylee777.github.io

 
[참고 블로그 2]
https://somjang.tistory.com/entry/Python%EB%A1%9C%EB%98%90-api%EB%A5%BC-%ED%99%9C%EC%9A%A9%ED%95%98%EC%97%AC-%EC%97%AD%EB%8C%80-%EB%A1%9C%EB%98%90-%EB%8B%B9%EC%B2%A8-%EA%B2%B0%EA%B3%BC-%EB%B6%84%EC%84%9D%ED%95%B4%EB%B3%B4%EA%B8%B0

[Python]역대 로또 당첨 번호 csv로 저장하고 분석해보기! (feat.나눔로또API)

로또, 다들 살면서 한번쯤은 구매하고 당첨 번호 발표일까지 당첨이 되면 무엇을 할까 고민하며 행복해 본 적이 있을 겁니다. 저도 가끔 구매 해보고 있지만 항상 결과는 역시나 였습니다. 그러

somjang.tistory.com

 


아니 진짜 .. 나는 .....
이 들여쓰기랑 오타랑 참 진촤.......

def get_max_count():
    url = 'https://dhlottery.co.kr/common.do?method=main'
    html = requests.get(url).text
    soup = BeautifulSoup(html, 'lxml')

    max_count = int(soup.find('strong', id='lottoDrwNo').text)
    return max_count

def lotto(count):
    url=f'https://dhlottery.co.kr/gameResult.do?method=byWin&drwNo={count}'
    html = requests.get(url).text
    soup1 = BeautifulSoup(html, 'lxml')

    # 날짜
    date = date_1.format('%Y년 %m월 %d일')
    # 당첨 번호
    win_num = [int(i) for i in soup1.find('div', class_ = 'num win').find('p').text.split()]
    # 보너스 번호
    b_num = int(soup1.find('div', class_ = 'num bonus').find('p').text)

    return {
        '날짜': date,
        '당첨번호': win_num,
        '보너스번호': b_num
    }

max_count = get_max_count()

dataf = {
    '날짜' : [],
    'num1' : [],
    'num2' : [],
    'num3' : [],
    'num4' : [],
    'num5' : [],
    'num6' : [],
    'bonus' : [],
    }

for i in tqdm(range(1, max_count+1)):
    result = lotto(i)

    dataf['날짜'].append(result['날짜'])
    dataf['num1'].append(result['당첨번호'][0])
    dataf['num2'].append(result['당첨번호'][1])
    dataf['num3'].append(result['당첨번호'][2])
    dataf['num4'].append(result['당첨번호'][3])
    dataf['num5'].append(result['당첨번호'][4])
    dataf['num6'].append(result['당첨번호'][5])
    dataf['bonus'].append(result['보너스번호'])

df1 = pd.DataFrame(dataf)
df1

물론 사이트에 들어가면
엑셀파일로 당첨번호에 대한 정보들 받을 수 있지만 나는 배웠던 사람이니까 ㅋㅋㅋㅋㅋ
크롤링으로 함 해봄 ㅋㅋㅋㅋ
다음엔 그런 자료 받아서 써야징
데이터프레임으로 저장해서 불러오면


중간 과정은 생략...(귀찮아서 그런거 아님ㅋㅋㅋㅋ)
당첨 번호로만 봤을때 (보너스 번호 제외)
상위 15개 번호 이렇게 나옴

보너스 번호 넣으니까 ㅋㅋ
인터넷에서 돌고도는 로또 많이 나온 번호들이 보였음
여기서 나는 20개만 뽑아서
랜덤으로 5개 로또 번호 만들어서 ㅋㅋㅋㅋ 샀음ㅋㅋㅋㅋㅋㅋㅋㅋ😊
친구도 같은 번호 달라던데.... 과연....


🌈 전체 코드

728x90