Python

Python 크롤링 샘플

지오준 2021. 2. 28.
반응형

일본의 로또번호 제공사이트에서 로또번호 크롤링하기

크롤링대상 사이트주소 : https://takarakuji.rakuten.co.jp/backnumber/

작업 1단계

①크롤링작업 페이지로 이동(https://takarakuji.rakuten.co.jp/backnumber/loto6/)

②크롤링작업 페이지의 소스확인(크롬의경우:view-source:https://takarakuji.rakuten.co.jp/backnumber/loto6/)

③크롤링작업 페이지 분석후 로또정보확인(로또횟차, 추첨일, 번호, 보너스번호)

<table class="tblType02 tblNumberGuid">
   <tbody>
    	<tr>
            <th class="tit">回号</th>           
            <th colspan="6">第1554回</th>       #로또횟차
        </tr>
        <tr>
            <th>抽せん日</th>
            <td colspan="6">2021/01/25</td>     #추첨일자
        </tr>
        <tr class="evenLine">
            <th>本数字</th>                      #번호
            <td><span class="loto-font-large">6</span></td>
            <td><span class="loto-font-large">14</span></td>
            <td><span class="loto-font-large">16</span></td>
            <td><span class="loto-font-large">27</span></td>
            <td><span class="loto-font-large">40</span></td>
            <td><span class="loto-font-large">42</span></td>
        </tr>
        <tr class="evenLine">
            <th>ボーナス数字</th>                #보너스번호
            <td><span class="loto-highlight loto-font-large">(26)</span></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th>1等</th>
            <td class="txtRight" colspan="3">2口</td>
            <td class="txtRight" colspan="3"><b>124,357,100円</b></td>
        </tr>
        <tr>
            <th>2等</th>
            <td class="txtRight" colspan="3">4口</td>
            <td class="txtRight" colspan="3"><b>18,654,100円</b></td>
        </tr>
        <tr>
            <th>3等</th>
            <td class="txtRight" colspan="3">191口</td>
            <td class="txtRight" colspan="3"><b>421,900円</b></td>
        </tr>
        <tr>
            <th>4等</th>
            <td class="txtRight" colspan="3">9,880口</td>
            <td class="txtRight" colspan="3"><b>8,600円</b></td>
        </tr>
        <tr>
            <th>5等</th>
            <td class="txtRight" colspan="3">167,174口</td>
            <td class="txtRight" colspan="3"><b>1,000円</b></td>
        </tr>
        <tr>
            <th>キャリーオーバー</th>
            <td class="txtRight" colspan="6"><b>0円</b></td>
        </tr>
     </tbody>
</table>

작업 2단계

①Python코드작성

import requests
from bs4 import BeautifulSoup 
from urllib.request import urlopen
import os
import datetime 

#로또정보크롤링 함수
def Loto6AutoRecentlyHtmlNew(url):
    result = ""

    #로또정보제공사이트주소에 접속후 Html정보얻어오기
    #"https://takarakuji.rakuten.co.jp/backnumber/loto6/"
    source_code = requests.get(url)
    source_code.encoding = source_code.apparent_encoding
    
    #로또정보제공사이트의 Html정보를 문자로변환
    plain_text = source_code.text

    #로또정보제공사이트의 문자정보를 Html형태로변환
    soup = BeautifulSoup(plain_text, 'html.parser')

    #로또정보를 테이블형태로 변환해서 출력
    result=""
    result += "<center>"
    result += "<h2>楽天×宝くじサイトからロート6の最近当選番号取得</h2>"
    result += "<table border='1'>"
    result += "<tr>"
    result += "<th>回号</th>"
    result += "<th>抽せん日</th>"
    result += "<th colspan='6'>本数字</th>"
    result += "<th>ボーナス数字</th>"
    result += "</tr>"

    #로또정보제공사이트의 Html형식의 정보를 분석후 로또정보만 가져와서 Table입력
    for table in soup.findAll('table', {'class': 'tblType02'}):
        rows = table.findAll("tr")
        result += "<tr>"
        rowsnumber = 0

        for row in rows:
            if rowsnumber == 0:             #로또회차 가져오기
                for cell in row.findAll('th',{'colspan': '6'}):
                    result += "<td>" + cell.text.replace("第","").replace("回","") + "</td>"
            elif rowsnumber == 1:           #로또번호 추점일 가져오기
                for cell in row.findAll('td',{'colspan': '6'}):
                    result += "<td>" + cell.text + "</td>"
            elif rowsnumber == 2:           #로또번호 가져오기
                for cell in row.findAll(['td']):
                    result += "<td>" + cell.text + "</td>"
            elif rowsnumber == 3:           #로또보너스번호 가져오기
                for cell in row.findAll(['td']):
                    if cell.text != "":
                        result += "<td>" + cell.text.replace("(","").replace(")","") + "</td>"      
            
            rowsnumber += 1      

        result += "<tr>"
        
    result += "</table>"
    result += "</center>"
        
    return result

②Python코드실행후 로또정보확인

반응형

댓글