読者です 読者をやめる 読者になる 読者になる

ロト6 最新結果取得

import datetime, locale, re
import urllib.request
from bs4 import BeautifulSoup

url = 'http://www.mizuhobank.co.jp/takarakuji/loto/loto6/index.html'

html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, from_encoding='cp932')

loto6_tousen = soup.find('table', class_='typeTK')
loto6_data = [i.get_text() for i in loto6_tousen.find_all('td')]

#不用データ削除
del loto6_data[8]

#第?回追加
loto6_data.insert(0,soup.find('th', colspan="6", class_="center bgf7f7f7").get_text())

print(loto6_data)

locale.setlocale(locale.LC_NUMERIC,'Japanese_Japan.932')

loto6_list = {'count' : int( re.sub(r'^第(\d+)回$', r'\1', loto6_data[0]) ),
             'date' : datetime.datetime.strptime(loto6_data[1], '%Y年%m月%d日'),
             'win_num' : loto6_data[2:8],
             'bou_num' : loto6_data[8].strip('()'),
             'prize_1st' : {'lot' : locale.atoi(loto6_data[9].rstrip('口')), 'prize': locale.atoi(loto6_data[10].rstrip('円'))},
             'prize_2nd' : {'lot' : locale.atoi(loto6_data[11].rstrip('口')), 'prize': locale.atoi(loto6_data[12].rstrip('円'))},
             'prize_3rd' : {'lot' : locale.atoi(loto6_data[13].rstrip('口')), 'prize': locale.atoi(loto6_data[14].rstrip('円'))},
             'prize_4th' : {'lot' : locale.atoi(loto6_data[15].rstrip('口')), 'prize': locale.atoi(loto6_data[16].rstrip('円'))},
             'prize_5th' : {'lot' : locale.atoi(loto6_data[17].rstrip('口')), 'prize': locale.atoi(loto6_data[18].rstrip('円'))},
             'sales_amount' : locale.atoi(loto6_data[19].rstrip('円')),
             'carry_over' : locale.atoi(loto6_data[20].rstrip('円'))}

print(loto6_list['count'], loto6_list['date'])
print(loto6_list['win_num'], loto6_list['bou_num'])