From 031d3fdee838ebed26912201a61f00593a5fb605 Mon Sep 17 00:00:00 2001 From: Cliff Su Date: Wed, 4 Oct 2023 23:25:15 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE-#38]=20feat:=20implement=2039=E6=A8=82?= =?UTF-8?q?=E5=90=88=E5=BD=A9=E6=9F=A5=E8=A9=A2=E7=B5=90=E6=9E=9C=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 32 ++++++++++++++++++++++---- TaiwanLottery/__init__.py | 48 +++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- tests/test_lottery.py | 37 ++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5febcc1..0a2b217 100644 --- a/README.md +++ b/README.md @@ -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 種彩券遊戲。 ## 功能 @@ -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) ``` @@ -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 @@ -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 diff --git a/TaiwanLottery/__init__.py b/TaiwanLottery/__init__.py index 28b29f1..03f93d1 100644 --- a/TaiwanLottery/__init__.py +++ b/TaiwanLottery/__init__.py @@ -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 = '查無資料' @@ -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): diff --git a/setup.py b/setup.py index e558fe4..969d50f 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name="taiwanlottery", - version="1.3.0", + version="1.4.0", author="Cliff Su", author_email="stu01509@gmail.com", description="Taiwan Lottery Crawler 台灣彩券爬蟲", diff --git a/tests/test_lottery.py b/tests/test_lottery.py index 6be35bb..8f26cac 100644 --- a/tests/test_lottery.py +++ b/tests/test_lottery.py @@ -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']}, + ]