Skip to content

Commit

Permalink
[ISSUE-#35] feat: implement 3星彩 search (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
stu01509 authored Sep 21, 2023
1 parent 4595bf0 commit 3e05931
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

## 介紹

這個專案是用來爬取 [台灣彩券](https://www.taiwanlottery.com.tw/) 官網上歷史的開獎紀錄,目前支援**威力彩****大樂透****今彩539****雙贏彩** 4 種彩券遊戲。
這個專案是用來爬取 [台灣彩券](https://www.taiwanlottery.com.tw/) 官網上歷史的開獎紀錄,目前支援**威力彩****大樂透****今彩539****雙贏彩****3星彩** 5 種彩券遊戲。

## 功能

- 爬取威力彩、大樂透、今彩539、雙贏彩 4 種彩券遊戲的開獎紀錄。
- 爬取威力彩、大樂透、今彩539、雙贏彩、3星彩 5 種彩券遊戲的開獎紀錄。

## 環境需求

Expand Down Expand Up @@ -72,6 +72,16 @@ result = lottery.lotto1224()
print(result)
```

[3星彩](https://codesandbox.io/p/sandbox/3xing-cai-dang-yue-fen-de-kai-jiang-ji-lu-vsxs3p)

```python
from TaiwanLottery import TaiwanLotteryCrawler

lottery = TaiwanLotteryCrawler()
result = lottery.lotto3d()
print(result)
```

---

### 爬取指定年月的開獎紀錄(民國年份)
Expand Down Expand Up @@ -116,12 +126,23 @@ result = lottery.lotto1224(['112', '6'])
print(result)
```

[3星彩](https://codesandbox.io/p/sandbox/3xing-cai-zhi-ding-nian-yue-de-kai-jiang-ji-lu-vsfvlx)

```python
from TaiwanLottery import TaiwanLotteryCrawler

lottery = TaiwanLotteryCrawler()
result = lottery.lotto3d(['112', '8'])
print(result)
```

## 資料來源

- [威力彩](https://www.taiwanlottery.com.tw/Lotto/SuperLotto638/history.aspx)
- [大樂透](https://www.taiwanlottery.com.tw/Lotto/Lotto649/history.aspx)
- [今彩539](https://www.taiwanlottery.com.tw/Lotto/Dailycash/history.aspx)
- [雙贏彩](https://www.taiwanlottery.com.tw/Lotto/Lotto1224/history.aspx)
- [3星彩](https://www.taiwanlottery.com.tw/Lotto/3D/history.aspx)

## License

Expand Down
61 changes: 58 additions & 3 deletions TaiwanLottery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import logging
import random
import time
import requests
from bs4 import BeautifulSoup
from TaiwanLottery import utils
import random


class TaiwanLotteryCrawler():
COUNT_OF_3D_LOTTERY_PRIZE_NUMBER = 3

html_parser = 'html.parser'
no_data = '查無資料'
Expand Down Expand Up @@ -116,6 +117,7 @@ def lotto649(self, back_time=[utils.get_current_republic_era(), utils.get_curren
"獎號": temp_second_nums,
"特別號": second_nums[i * 2].text.strip()
}

datas.append(data)

if len(datas) == 0:
Expand Down Expand Up @@ -232,7 +234,60 @@ def lotto1224(self, back_time=[utils.get_current_republic_era(), utils.get_curre

return datas

# 3星彩
def lotto3d(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]):
URL = 'https://www.taiwanlottery.com.tw/Lotto/3D/history.aspx'
title = '3星彩_' + str(back_time[0]) + '_' + str(back_time[1])

res = requests.get(URL)
soup = BeautifulSoup(res.text, self.html_parser)
datas = []

payload = {
'L3DControl_history1$chk': 'radYM',
'L3DControl_history1$dropYear': back_time[0],
'L3DControl_history1$dropMonth': back_time[1],
'L3DControl_history1$btnSubmit': '查詢'
}
payload["__VIEWSTATE"] = soup.select_one("#__VIEWSTATE")["value"]
payload["__VIEWSTATEGENERATOR"] = soup.select_one(
"#__VIEWSTATEGENERATOR")["value"]
payload["__EVENTVALIDATION"] = soup.select_one(
"#__EVENTVALIDATION")["value"]

res = requests.post(URL, data=payload)
soup = BeautifulSoup(res.text, self.html_parser)

if (self.no_data in res.text):
logging.warning(self.no_data + title)
return

first_nums = soup.select(".td_w.font_black14b_center")
data_count = len(first_nums) / self.COUNT_OF_3D_LOTTERY_PRIZE_NUMBER
stage = soup.select('table[class*="table_"] > tr:nth-child(3) > td:nth-child(1)')
date = soup.select('table[class*="table_"] > tr:nth-child(3) > td:nth-child(2) > p')

for i in range(0, int(data_count)):
temp_second_nums = []

for j in range(self.COUNT_OF_3D_LOTTERY_PRIZE_NUMBER):
temp_second_nums.append(first_nums[((i) * self.COUNT_OF_3D_LOTTERY_PRIZE_NUMBER) + j].text.strip())

data = {
"期別": stage[i].text,
"開獎日期": date[i].text,
"獎號": temp_second_nums,
}
datas.append(data)

if len(datas) == 0:
logging.warning(self.no_data + title)
return

return datas

# 威力彩歷史查詢

def super_lotto_back(self, back_month='0'):
for i in range(int(back_month), -1, -1):
time.sleep(random.random())
Expand Down
38 changes: 38 additions & 0 deletions tests/test_lottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,41 @@ def test_lotto1224():
{'期別': '112000131', '開獎日期': '112/06/02', '獎號': ['08', '12', '24', '01', '07', '15', '11', '18', '19', '06', '21', '14']},
{'期別': '112000130', '開獎日期': '112/06/01', '獎號': ['23', '17', '10', '14', '20', '03', '05', '09', '15', '12', '07', '19']}
]


def test_lotto3d():
# Given user wants to get 3星彩 2023-08 result
lottery = TaiwanLotteryCrawler()

# When user get the 3星彩 2023-08 result without print and output to json
lotto3d_result = lottery.lotto3d([112, 8])

assert lotto3d_result == [
{'期別': '112000208', '開獎日期': '開獎112/8/31', '獎號': ['7', '2', '7']},
{'期別': '112000207', '開獎日期': '開獎112/8/30', '獎號': ['9', '0', '1']},
{'期別': '112000206', '開獎日期': '開獎112/8/29', '獎號': ['1', '5', '9']},
{'期別': '112000205', '開獎日期': '開獎112/8/28', '獎號': ['5', '6', '6']},
{'期別': '112000204', '開獎日期': '開獎112/8/26', '獎號': ['5', '8', '0']},
{'期別': '112000203', '開獎日期': '開獎112/8/25', '獎號': ['9', '7', '1']},
{'期別': '112000202', '開獎日期': '開獎112/8/24', '獎號': ['7', '0', '4']},
{'期別': '112000201', '開獎日期': '開獎112/8/23', '獎號': ['6', '3', '2']},
{'期別': '112000200', '開獎日期': '開獎112/8/22', '獎號': ['1', '5', '2']},
{'期別': '112000199', '開獎日期': '開獎112/8/21', '獎號': ['0', '9', '7']},
{'期別': '112000198', '開獎日期': '開獎112/8/19', '獎號': ['7', '4', '3']},
{'期別': '112000197', '開獎日期': '開獎112/8/18', '獎號': ['9', '8', '6']},
{'期別': '112000196', '開獎日期': '開獎112/8/17', '獎號': ['2', '7', '7']},
{'期別': '112000195', '開獎日期': '開獎112/8/16', '獎號': ['3', '1', '6']},
{'期別': '112000194', '開獎日期': '開獎112/8/15', '獎號': ['6', '4', '8']},
{'期別': '112000193', '開獎日期': '開獎112/8/14', '獎號': ['8', '9', '5']},
{'期別': '112000192', '開獎日期': '開獎112/8/12', '獎號': ['2', '1', '5']},
{'期別': '112000191', '開獎日期': '開獎112/8/11', '獎號': ['6', '1', '7']},
{'期別': '112000190', '開獎日期': '開獎112/8/10', '獎號': ['2', '7', '2']},
{'期別': '112000189', '開獎日期': '開獎112/8/9', '獎號': ['6', '8', '6']},
{'期別': '112000188', '開獎日期': '開獎112/8/8', '獎號': ['8', '0', '5']},
{'期別': '112000187', '開獎日期': '開獎112/8/7', '獎號': ['1', '2', '7']},
{'期別': '112000186', '開獎日期': '開獎112/8/5', '獎號': ['4', '7', '2']},
{'期別': '112000185', '開獎日期': '開獎112/8/4', '獎號': ['5', '0', '4']},
{'期別': '112000184', '開獎日期': '開獎112/8/3', '獎號': ['7', '4', '0']},
{'期別': '112000183', '開獎日期': '開獎112/8/2', '獎號': ['4', '0', '4']},
{'期別': '112000182', '開獎日期': '開獎112/8/1', '獎號': ['9', '0', '1']}
]

0 comments on commit 3e05931

Please sign in to comment.