张延森 4 years ago
parent
commit
77f27f092e

BIN
crawl/comm/__pycache__/lottery.cpython-38.pyc View File


+ 32
- 0
crawl/comm/lottery.py View File

@@ -0,0 +1,32 @@
1
+from .mydb import MyDB
2
+
3
+class LotteryResult:
4
+  def __init__(self,
5
+               # 类型
6
+               lotteryType,
7
+
8
+               # 期数
9
+               issueNo,
10
+
11
+               # 号码
12
+               result
13
+               ):
14
+    self.lotteryType = lotteryType
15
+    self.issueNo = issueNo
16
+    self.result = result
17
+
18
+  def persist(self):
19
+    cursor = MyDB.getCursor()
20
+    if cursor is None:
21
+      return
22
+
23
+    query = "select count(*) from ta_crawl_lottery_result where lottery_type = %s and issue_no = %s and status = 1"
24
+    cursor.execute(query,[self.lotteryType, self.issueNo])
25
+    res = cursor.fetchone()
26
+    if res[0] > 0:
27
+      return
28
+
29
+    sql = "insert into ta_crawl_lottery_result(lottery_type, issue_no, result) values(%s, %s, %s)"
30
+    cursor.execute(sql, [self.lotteryType, self.issueNo, self.result])
31
+    MyDB.commit()
32
+    cursor.close()

BIN
crawl/spiders/__pycache__/lottery.cpython-38.pyc View File


+ 60
- 0
crawl/spiders/lottery.py View File

@@ -0,0 +1,60 @@
1
+import scrapy
2
+
3
+from crawl.comm.lottery import LotteryResult
4
+
5
+
6
+class LotterySpider(scrapy.Spider):
7
+  name = "lottery"
8
+
9
+  def start_requests(self):
10
+    url = "https://www.lottery.gov.cn/tz_kj.json"
11
+    yield scrapy.Request(url, self.parseLottery)
12
+
13
+    # 福彩
14
+    url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice"
15
+    # 双色球
16
+    yield scrapy.Request(url + "?name=ssq&issueCount=1", self.parseDraw)
17
+
18
+  # 体彩
19
+  def parseLottery(self, response):
20
+    data = response.json()
21
+    data = data[0]
22
+
23
+    # 大乐透
24
+    key = 'dlt'
25
+    typ = 'lottery'
26
+    LotteryResult(
27
+      typ,
28
+      data[key]['term'],
29
+      '|'.join(data[key]['numberCode'])
30
+    ).persist()
31
+
32
+    # 排列三
33
+    key = 'pls'
34
+    typ = 'p3'
35
+    LotteryResult(
36
+      typ,
37
+      data[key]['term'],
38
+      '|'.join(data[key]['numberCode'])
39
+    ).persist()
40
+
41
+    # 排列五w
42
+    key = 'plw'
43
+    typ = 'p5'
44
+    LotteryResult(
45
+      typ,
46
+      data[key]['term'],
47
+      '|'.join(data[key]['numberCode'])
48
+    ).persist()
49
+
50
+  # 福彩 - 双色球
51
+  def parseDraw(self, response):
52
+    data = response.json()
53
+    result = data['result'][0]
54
+
55
+    typ = 'double-color'
56
+    LotteryResult(
57
+      typ,
58
+      result['code'],
59
+      '|'.join(result['red'].split(',') + [result['blue']])
60
+    ).persist()

+ 1
- 1
main.py View File

@@ -4,4 +4,4 @@ import os
4 4
 
5 5
 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
6 6
 
7
-execute(['scrapy', 'crawl', 'basketball-result'])  # 你需要将此处的spider_name替换为你自己的爬虫名称
7
+execute(['scrapy', 'crawl', 'lottery'])  # 你需要将此处的spider_name替换为你自己的爬虫名称