Skip to content

Commit

Permalink
[ISSUE-#38] feat: implement 39樂合彩查詢結果 search (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
stu01509 authored Oct 4, 2023
1 parent 19b2cad commit c4f6aee
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## 介紹

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

## 功能

Expand Down Expand Up @@ -92,23 +92,33 @@ result = lottery.lotto4d()
print(result)
```

[49樂合彩](https://codesandbox.io/p/sandbox/49le-he-cai-dang-yue-fen-de-kai-jiang-ji-lu-jgy94n)
[38樂合彩](https://codesandbox.io/p/sandbox/38le-he-cai-dang-yue-fen-de-kai-jiang-ji-lu-yfphxf)

```python
from TaiwanLottery import TaiwanLotteryCrawler

lottery = TaiwanLotteryCrawler()
result = lottery.lotto49m6()
result = lottery.lotto38m6()
print(result)
```

[38樂合彩](https://codesandbox.io/p/sandbox/38le-he-cai-dang-yue-fen-de-kai-jiang-ji-lu-yfphxf)
[39樂合彩](https://codesandbox.io/p/sandbox/39le-he-cai-dang-yue-fen-de-kai-jiang-ji-lu-mgqwfg)

```python
from TaiwanLottery import TaiwanLotteryCrawler

lottery = TaiwanLotteryCrawler()
result = lottery.lotto38m6()
result = lottery.lotto39m5()
print(result)
```

[49樂合彩](https://codesandbox.io/p/sandbox/49le-he-cai-dang-yue-fen-de-kai-jiang-ji-lu-jgy94n)

```python
from TaiwanLottery import TaiwanLotteryCrawler

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

Expand Down Expand Up @@ -186,6 +196,16 @@ result = lottery.lotto38m6(['112', '8'])
print(result)
```

[39樂合彩](https://codesandbox.io/p/sandbox/39le-he-cai-zhi-ding-nian-yue-de-kai-jiang-ji-lu-lskqmm)

```python
from TaiwanLottery import TaiwanLotteryCrawler

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

[49樂合彩](https://codesandbox.io/p/sandbox/49le-he-cai-zhi-ding-nian-yue-de-kai-jiang-ji-lu-ff6d5d)

```python
Expand All @@ -205,6 +225,8 @@ print(result)
- [3星彩](https://www.taiwanlottery.com.tw/Lotto/3D/history.aspx)
- [4星彩](https://www.taiwanlottery.com.tw/Lotto/4D/history.aspx)
- [38樂合彩](https://www.taiwanlottery.com.tw/Lotto/38m6/history.aspx)
- [39樂合彩](https://www.taiwanlottery.com.tw/Lotto/39m5/history.aspx)
- [49 樂合彩](https://www.taiwanlottery.com.tw/Lotto/49m6/history.aspx)

## License

Expand Down
48 changes: 48 additions & 0 deletions TaiwanLottery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TaiwanLotteryCrawler():
COUNT_OF_4D_LOTTERY_PRIZE_NUMBER = 4
COUNT_OF_38M6_LOTTERY_PRIZE_NUMBER = 6
COUNT_OF_49M6_LOTTERY_PRIZE_NUMBER = 6
COUNT_OF_39M5_LOTTERY_PRIZE_NUMBER = 5

html_parser = 'html.parser'
no_data = '查無資料'
Expand Down Expand Up @@ -410,6 +411,53 @@ def lotto49m6(self, back_time=[utils.get_current_republic_era(), utils.get_curre

return datas

# 39樂合彩
def lotto39m5(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]):
URL = 'https://www.taiwanlottery.com.tw/Lotto/39m5/history.aspx'
title = '39樂合彩_' + str(back_time[0]) + '_' + str(back_time[1])

datas = []
payload = {
'M539Control_history1$chk': 'radYM',
'M539Control_history1$dropYear': back_time[0],
'M539Control_history1$dropMonth': back_time[1],
'M539Control_history1$btnSubmit': '查詢',
}

self.initial_default_payload(URL, payload, ['__VIEWSTATE', '__VIEWSTATEGENERATOR', '__EVENTVALIDATION', '__VIEWSTATEENCRYPTED'])

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 > span")
data_count = len(first_nums) / self.COUNT_OF_39M5_LOTTERY_PRIZE_NUMBER / 2

stage = soup.select('table[class*="table_"] tr:nth-child(2) > td:nth-child(1) > span')
date = soup.select('table[class*="table_"] tr:nth-child(2) > td:nth-child(2) > span > span')

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

for j in range(self.COUNT_OF_39M5_LOTTERY_PRIZE_NUMBER):
temp_second_nums.append(first_nums[((i * 2) * self.COUNT_OF_39M5_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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="taiwanlottery",
version="1.3.0",
version="1.4.0",
author="Cliff Su",
author_email="[email protected]",
description="Taiwan Lottery Crawler 台灣彩券爬蟲",
Expand Down
37 changes: 37 additions & 0 deletions tests/test_lottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,40 @@ def test_lotto49m6():
{'期別': '112000066', '開獎日期': '112/07/07', '獎號': ['24', '21', '03', '33', '15', '06']},
{'期別': '112000065', '開獎日期': '112/07/04', '獎號': ['41', '06', '15', '24', '12', '16']},
]


def test_lotto39m5():
# Given user wants to get 39樂合彩 2023-07 result
lottery = TaiwanLotteryCrawler()

# When user get the 39樂合彩 2023-07 result without print and output to json
lotto39m5_result = lottery.lotto39m5([112, 7])

assert lotto39m5_result == [
{'期別': '112000181', '開獎日期': '112/07/31', '獎號': ['13', '21', '03', '19', '35']},
{'期別': '112000180', '開獎日期': '112/07/29', '獎號': ['37', '30', '18', '15', '20']},
{'期別': '112000179', '開獎日期': '112/07/28', '獎號': ['20', '26', '07', '01', '13']},
{'期別': '112000178', '開獎日期': '112/07/27', '獎號': ['13', '39', '10', '18', '37']},
{'期別': '112000177', '開獎日期': '112/07/26', '獎號': ['37', '13', '06', '33', '38']},
{'期別': '112000176', '開獎日期': '112/07/25', '獎號': ['15', '32', '18', '26', '19']},
{'期別': '112000175', '開獎日期': '112/07/24', '獎號': ['31', '34', '02', '37', '13']},
{'期別': '112000174', '開獎日期': '112/07/22', '獎號': ['15', '32', '04', '09', '24']},
{'期別': '112000173', '開獎日期': '112/07/21', '獎號': ['27', '26', '31', '39', '09']},
{'期別': '112000172', '開獎日期': '112/07/20', '獎號': ['23', '13', '20', '18', '31']},
{'期別': '112000171', '開獎日期': '112/07/19', '獎號': ['02', '35', '22', '12', '20']},
{'期別': '112000170', '開獎日期': '112/07/18', '獎號': ['23', '29', '38', '15', '12']},
{'期別': '112000169', '開獎日期': '112/07/17', '獎號': ['29', '01', '22', '18', '30']},
{'期別': '112000168', '開獎日期': '112/07/15', '獎號': ['08', '22', '19', '21', '16']},
{'期別': '112000167', '開獎日期': '112/07/14', '獎號': ['14', '25', '37', '12', '20']},
{'期別': '112000166', '開獎日期': '112/07/13', '獎號': ['19', '06', '10', '32', '04']},
{'期別': '112000165', '開獎日期': '112/07/12', '獎號': ['09', '01', '32', '02', '10']},
{'期別': '112000164', '開獎日期': '112/07/11', '獎號': ['35', '38', '25', '24', '36']},
{'期別': '112000163', '開獎日期': '112/07/10', '獎號': ['15', '04', '19', '37', '01']},
{'期別': '112000162', '開獎日期': '112/07/08', '獎號': ['33', '24', '01', '18', '04']},
{'期別': '112000161', '開獎日期': '112/07/07', '獎號': ['30', '16', '12', '25', '28']},
{'期別': '112000160', '開獎日期': '112/07/06', '獎號': ['21', '17', '02', '33', '14']},
{'期別': '112000159', '開獎日期': '112/07/05', '獎號': ['05', '08', '07', '25', '30']},
{'期別': '112000158', '開獎日期': '112/07/04', '獎號': ['14', '27', '29', '26', '35']},
{'期別': '112000157', '開獎日期': '112/07/03', '獎號': ['28', '34', '33', '39', '13']},
{'期別': '112000156', '開獎日期': '112/07/01', '獎號': ['12', '22', '08', '39', '07']},
]

0 comments on commit c4f6aee

Please sign in to comment.