Repository: stu01509/TaiwanLotteryCrawler (language: python)

Issue:
FEAT: 實作 38樂合彩查詢結果
### 問題 Issue

FEAT: 實作 38樂合彩查詢結果

### 重現步驟 Reproduce

重現該行為的步驟(Steps to reproduce the behavior):
1. 執行 '....'
2. See error
3. ...

### 預期行為 Expected Behavior


### 環境 Environment

- Python Version:
- Operating System:
- TaiwanLotteryCrawler Version:

### 截圖 Screenshot


Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index f6c5d87..a4241cc 100644
--- a/README.md
+++ b/README.md
@@ -12,11 +12,11 @@
 
 ## 介紹
 
-這個專案是用來爬取 [台灣彩券](https://www.taiwanlottery.com.tw/) 官網上歷史的開獎紀錄，目前支援**威力彩**、**大樂透**、**今彩539**、**雙贏彩**、**3星彩**、**4星彩** 6 種彩券遊戲。
+這個專案是用來爬取 [台灣彩券](https://www.taiwanlottery.com.tw/) 官網上歷史的開獎紀錄，目前支援**威力彩**、**大樂透**、**今彩539**、**雙贏彩**、**3星彩**、**4星彩**、**38樂合彩** 7 種彩券遊戲。
 
 ## 功能
 
-- 爬取威力彩、大樂透、今彩539、雙贏彩、3星彩、4星彩 6 種彩券遊戲的開獎紀錄。
+- 爬取威力彩、大樂透、今彩539、雙贏彩、3星彩、4星彩、38樂合彩 7 種彩券遊戲的開獎紀錄。
 
 ## 環境需求
 
@@ -92,6 +92,16 @@ result = lottery.lotto4d()
 print(result)
 ```
 
+[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.lotto38m6()
+print(result)
+```
+
 ---
 
 ### 爬取指定年月的開獎紀錄(民國年份)
@@ -156,6 +166,16 @@ result = lottery.lotto4d(['112', '8'])
 print(result)
 ```
 
+[38樂合彩](https://codesandbox.io/p/sandbox/38le-he-cai-zhi-ding-nian-yue-de-kai-jiang-ji-lu-m4s9jn)
+
+```python
+from TaiwanLottery import TaiwanLotteryCrawler
+
+lottery = TaiwanLotteryCrawler()
+result = lottery.lotto38m6(['112', '8'])
+print(result)
+```
+
 ## 資料來源
 
 - [威力彩](https://www.taiwanlottery.com.tw/Lotto/SuperLotto638/history.aspx)
@@ -164,6 +184,7 @@ print(result)
 - [雙贏彩](https://www.taiwanlottery.com.tw/Lotto/Lotto1224/history.aspx)
 - [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)
 
 ## License
 
diff --git a/TaiwanLottery/__init__.py b/TaiwanLottery/__init__.py
index 388877a..238f880 100644
--- a/TaiwanLottery/__init__.py
+++ b/TaiwanLottery/__init__.py
@@ -2,14 +2,17 @@
 import logging
 import random
 import time
+
 import requests
 from bs4 import BeautifulSoup
+
 from TaiwanLottery import utils
 
 
 class TaiwanLotteryCrawler():
     COUNT_OF_3D_LOTTERY_PRIZE_NUMBER = 3
     COUNT_OF_4D_LOTTERY_PRIZE_NUMBER = 4
+    COUNT_OF_38M6_LOTTERY_PRIZE_NUMBER = 6
 
     html_parser = 'html.parser'
     no_data = '查無資料'
@@ -289,7 +292,6 @@ def lotto3d(self, back_time=[utils.get_current_republic_era(), utils.get_current
 
     # 4星彩
     def lotto4d(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]):
-        print('------------------- lotto4d ------------------')
         URL = 'https://www.taiwanlottery.com.tw/Lotto/4D/history.aspx'
         title = '4星彩_' + str(back_time[0]) + '_' + str(back_time[1])
 
@@ -340,6 +342,63 @@ def lotto4d(self, back_time=[utils.get_current_republic_era(), utils.get_current
 
         return datas
 
+    # 38樂合彩
+    def lotto38m6(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]):
+        URL = 'https://www.taiwanlottery.com.tw/Lotto/38m6/history.aspx'
+        title = '38樂合彩_' + str(back_time[0]) + '_' + str(back_time[1])
+
+        res = requests.get(URL)
+        soup = BeautifulSoup(res.text, self.html_parser)
+
+        datas = []
+
+        payload = {
+            'M638Control_history1$chk': 'radYM',
+            'M638Control_history1$dropYear': back_time[0],
+            'M638Control_history1$dropMonth': back_time[1],
+            'M638Control_history1$btnSubmit': '查詢',
+        }
+
+        payload["__VIEWSTATEENCRYPTED"] = soup.select_one("#__VIEWSTATEENCRYPTED")["value"]
+        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 > span")
+        data_count = len(first_nums) / self.COUNT_OF_38M6_LOTTERY_PRIZE_NUMBER / 2
+
+        for i in range(0, int(data_count)):
+            temp_second_nums = []
+            stage = soup.select(
+                '#M638Control_history1_dlQuery_lbM638_DrawTerm_' + str(i))
+            date = soup.select(
+                '#M638Control_history1_dlQuery_M638_DDate_' + str(i))
+
+            for j in range(self.COUNT_OF_38M6_LOTTERY_PRIZE_NUMBER):
+                temp_second_nums.append(first_nums[((i * 2) * self.COUNT_OF_38M6_LOTTERY_PRIZE_NUMBER) + j].text.strip())
+
+            data = {
+                "期別": stage[0].text,
+                "開獎日期": date[0].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):
diff --git a/setup.py b/setup.py
index b739dba..e558fe4 100644
--- a/setup.py
+++ b/setup.py
@@ -8,7 +8,7 @@
 
 setup(
     name="taiwanlottery",
-    version="1.2.0",
+    version="1.3.0",
     author="Cliff Su",
     author_email="stu01509@gmail.com",
     description="Taiwan Lottery Crawler 台灣彩券爬蟲",


Test patch:
diff --git a/tests/test_lottery.py b/tests/test_lottery.py
index a392cc9..15fc868 100644
--- a/tests/test_lottery.py
+++ b/tests/test_lottery.py
@@ -194,3 +194,23 @@ def test_lotto4d():
         {'期別': '112000183', '開獎日期': '112/8/2', '獎號': ['5', '5', '5', '1']},
         {'期別': '112000182', '開獎日期': '112/8/1', '獎號': ['5', '0', '9', '3']},
     ]
+
+
+def test_lotto38m6():
+    # Given user wants to get 38樂合彩 2023-08 result
+    lottery = TaiwanLotteryCrawler()
+
+    # When user get the 38樂合彩 2023-08 result without print and output to json
+    lotto38m6_result = lottery.lotto38m6([112, 8])
+
+    assert lotto38m6_result == [
+        {'期別': '112000070', '開獎日期': '112/08/31', '獎號': ['30', '34', '27', '23', '04', '22']},
+        {'期別': '112000069', '開獎日期': '112/08/28', '獎號': ['03', '08', '06', '15', '38', '24']},
+        {'期別': '112000068', '開獎日期': '112/08/24', '獎號': ['21', '03', '23', '17', '38', '10']},
+        {'期別': '112000067', '開獎日期': '112/08/21', '獎號': ['33', '19', '01', '12', '34', '14']},
+        {'期別': '112000066', '開獎日期': '112/08/17', '獎號': ['04', '37', '18', '28', '23', '21']},
+        {'期別': '112000065', '開獎日期': '112/08/14', '獎號': ['17', '04', '03', '26', '02', '10']},
+        {'期別': '112000064', '開獎日期': '112/08/10', '獎號': ['03', '29', '21', '30', '38', '11']},
+        {'期別': '112000063', '開獎日期': '112/08/07', '獎號': ['33', '29', '37', '03', '27', '31']},
+        {'期別': '112000062', '開獎日期': '112/08/03', '獎號': ['25', '17', '33', '18', '27', '21']}
+    ]