张延森 před 4 roky
rodič
revize
55661223c6

+ 22
- 1
.vscode/settings.json Zobrazit soubor

@@ -1,3 +1,24 @@
1 1
 {
2
-  "python.pythonPath": "d:/Application/python-3.8.5/python.exe"
2
+  "python.pythonPath": "d:/Application/python-3.8.5/python.exe",
3
+  "deploy": {
4
+    "targets": [
5
+      {
6
+        "type": "sftp",
7
+        "name": "NiuCai Crawl Deploy",
8
+        "description": "Deploy crawler to 130",
9
+        "dir": "${workspaceRoot}",
10
+        "host": "47.101.36.130", "port": 22,
11
+        "user": "root", "password": "$w-<[n{d!(sz(A.:3&u5vY?sm%6c0e",
12
+
13
+        "checkBeforeDeploy": true,
14
+
15
+        "mappings": [
16
+            {
17
+                "source": "${workspaceRoot}",
18
+                "target": "/opt/niucai/crawl"
19
+            }
20
+        ]
21
+      }
22
+    ]
23
+  }
3 24
 }

+ 14
- 0
basketball_job.sh Zobrazit soubor

@@ -0,0 +1,14 @@
1
+#/bin/sh
2
+#
3
+# 足彩,每 5 分钟执行一次
4
+
5
+source /etc/profile
6
+
7
+cd /opt/niucai/crawl
8
+
9
+# 赛事
10
+scrapy crawl basketball-match >> logs/basketball-match.log
11
+# 赔率
12
+scrapy crawl basketball-price >> logs/basketball-price.log
13
+# 结果
14
+scrapy crawl basketball-result >> logs/basketball-result.log

binární
crawl/comm/__pycache__/lottery.cpython-38.pyc Zobrazit soubor


+ 39
- 2
crawl/comm/lottery.py Zobrazit soubor

@@ -8,12 +8,21 @@ class LotteryResult:
8 8
                # 期数
9 9
                issueNo,
10 10
 
11
+               # 开奖日期
12
+               openingDate,
13
+
11 14
                # 号码
12 15
                result
13 16
                ):
14 17
     self.lotteryType = lotteryType
15 18
     self.issueNo = issueNo
19
+    self.openingDate = openingDate
16 20
     self.result = result
21
+    # 中奖详情
22
+    self.details = None
23
+
24
+  def setDetails(self, details):
25
+    self.details = details
17 26
 
18 27
   def persist(self):
19 28
     cursor = MyDB.getCursor()
@@ -26,7 +35,35 @@ class LotteryResult:
26 35
     if res[0] > 0:
27 36
       return
28 37
 
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])
38
+    sql = "insert into ta_crawl_lottery_result(lottery_type, issue_no, opening_date, result, detail) values(%s, %s, %s, %s, %s)"
39
+    detailStr = ""
40
+    if self.details is not None:
41
+      detailStr = ";".join(map(lambda x: x.toString(), self.details))
42
+
43
+    cursor.execute(sql, (self.lotteryType, self.issueNo, self.openingDate, self.result, detailStr))
31 44
     MyDB.commit()
32 45
     cursor.close()
46
+
47
+class LotteryResultDetail:
48
+  def __init__(self,
49
+               # 中奖级别
50
+               level,
51
+
52
+               # 注数
53
+               piece,
54
+
55
+               # 单注奖金
56
+               money):
57
+    self.level = level
58
+    self.piece = piece
59
+    self.money = money
60
+    # 额外追加
61
+    self.xPiece = "0"
62
+    self.xMoney = "0"
63
+
64
+  def add(self, xPiece, xMoney):
65
+    self.xPiece = xPiece
66
+    self.xMoney = xMoney
67
+
68
+  def toString(self):
69
+    return "|".join((self.level, self.piece, self.money, self.xPiece, self.xMoney))

+ 1
- 1
crawl/run.py Zobrazit soubor

@@ -7,4 +7,4 @@ print(dirpath)
7 7
 # 添加环境变量
8 8
 sys.path.append(dirpath)
9 9
 # 启动爬虫,第三个参数为爬虫name
10
-execute(['scrapy','crawl','football-price'])
10
+execute(['scrapy','crawl','lottery'])

binární
crawl/spiders/__pycache__/lottery.cpython-38.pyc Zobrazit soubor


+ 91
- 28
crawl/spiders/lottery.py Zobrazit soubor

@@ -1,60 +1,123 @@
1 1
 import scrapy
2 2
 
3
-from crawl.comm.lottery import LotteryResult
3
+from crawl.comm.lottery import LotteryResult, LotteryResultDetail
4 4
 
5 5
 
6 6
 class LotterySpider(scrapy.Spider):
7 7
   name = "lottery"
8 8
 
9 9
   def start_requests(self):
10
-    url = "https://www.lottery.gov.cn/tz_kj.json"
11
-    yield scrapy.Request(url, self.parseLottery)
10
+    # 大乐透
11
+    url = "https://www.lottery.gov.cn/api/lottery_kj_detail_new.jspx?_ltype=4"
12
+    yield scrapy.Request(url, self.parseLottery, 'POST')
13
+
14
+    # 排列三, 排列五
15
+    url = "https://www.lottery.gov.cn/api/lottery_kj_detail_new.jspx?_ltype=5"
16
+    yield scrapy.Request(url, self.parsePermutation, 'POST')
12 17
 
13
-    # 福彩
14
-    url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice"
15 18
     # 双色球
16
-    yield scrapy.Request(url + "?name=ssq&issueCount=1", self.parseDraw)
19
+    url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=1"
20
+    yield scrapy.Request(url, self.parseDraw)
17 21
 
18
-  # 体彩
22
+  # 大乐透
19 23
   def parseLottery(self, response):
20 24
     data = response.json()
21 25
     data = data[0]
22 26
 
23 27
     # 大乐透
24
-    key = 'dlt'
25 28
     typ = 'lottery'
26
-    LotteryResult(
29
+    result = LotteryResult(
27 30
       typ,
28
-      data[key]['term'],
29
-      '|'.join(data[key]['numberCode'])
30
-    ).persist()
31
+      data['lottery']['term'],
32
+      data['lottery']['fTime'][0:10],
33
+      '|'.join(data['codeNumber'])
34
+    )
35
+
36
+    details = []
37
+    for i, item in enumerate(data['details']):
38
+      detail = LotteryResultDetail(item.get('level'), item.get('piece'), item.get('money'))
39
+      detail.add(data['detailsAdd'][i].get('piece'), data['detailsAdd'][i].get('money'))
40
+      details.append(detail)
41
+    result.setDetails(details)
42
+
43
+    result.persist()
44
+
45
+  # 排列三, 排列五
46
+  def parsePermutation(self, response):
47
+    data = response.json()
48
+    data = data[0]
49
+
50
+    issueNo = data['lottery']['term']
51
+    openingDate = data['lottery']['fTime'][0:10]
31 52
 
32 53
     # 排列三
33
-    key = 'pls'
34 54
     typ = 'p3'
35
-    LotteryResult(
55
+    result = LotteryResult(
36 56
       typ,
37
-      data[key]['term'],
38
-      '|'.join(data[key]['numberCode'])
39
-    ).persist()
57
+      issueNo,
58
+      openingDate,
59
+      '|'.join(data['codeNumber'])
60
+    )
40 61
 
41
-    # 排列五w
42
-    key = 'plw'
62
+    details = []
63
+    for i, item in enumerate(data['details']):
64
+      detail = LotteryResultDetail(item.get('level'), item.get('piece'), item.get('money'))
65
+      details.append(detail)
66
+
67
+    result.setDetails(details)
68
+    result.persist()
69
+
70
+    # 排列五
43 71
     typ = 'p5'
44
-    LotteryResult(
72
+    result = LotteryResult(
45 73
       typ,
46
-      data[key]['term'],
47
-      '|'.join(data[key]['numberCode'])
48
-    ).persist()
74
+      issueNo,
75
+      openingDate,
76
+      '|'.join(data['codeNumberPlw'])
77
+    )
78
+
79
+    details = []
80
+    for i, item in enumerate(data['detailsPlw']):
81
+      detail = LotteryResultDetail(item.get('level'), item.get('piece'), item.get('money'))
82
+      details.append(detail)
83
+
84
+    result.setDetails(details)
85
+    result.persist()
49 86
 
50 87
   # 福彩 - 双色球
51 88
   def parseDraw(self, response):
52 89
     data = response.json()
53
-    result = data['result'][0]
90
+    data = data['result'][0]
54 91
 
55 92
     typ = 'double-color'
56
-    LotteryResult(
93
+    result = LotteryResult(
57 94
       typ,
58
-      result['code'],
59
-      '|'.join(result['red'].split(',') + [result['blue']])
60
-    ).persist()
95
+      data['code'],
96
+      data['date'][0:10],
97
+      '|'.join(data['red'].split(',') + [data['blue']])
98
+    )
99
+
100
+    details = []
101
+    for i, item in enumerate(data['prizegrades']):
102
+      level = item.get('type')
103
+      if level == 1:
104
+        level = '一等奖'
105
+      elif level == 2:
106
+        level = '二等奖'
107
+      elif level == 3:
108
+        level = '三等奖'
109
+      elif level == 4:
110
+        level = '四等奖'
111
+      elif level == 5:
112
+        level = '五等奖'
113
+      elif level == 6:
114
+        level = '六等奖'
115
+      else:
116
+        continue
117
+
118
+      detail = LotteryResultDetail(level, item.get('typenum'), item.get('typemoney'))
119
+      details.append(detail)
120
+
121
+    result.setDetails(details)
122
+    result.persist()
123
+

+ 14
- 0
football_job.sh Zobrazit soubor

@@ -0,0 +1,14 @@
1
+#/bin/sh
2
+#
3
+# 足彩,每 5 分钟执行一次
4
+
5
+source /etc/profile
6
+
7
+cd /opt/niucai/crawl
8
+
9
+# 赛事
10
+scrapy crawl football-match >> logs/football-match.log
11
+# 赔率
12
+scrapy crawl football-price >> logs/football-price.log
13
+# 结果
14
+scrapy crawl football-result >> logs/football-result.log

+ 9
- 0
lottery_job.sh Zobrazit soubor

@@ -0,0 +1,9 @@
1
+#/bin/sh
2
+#
3
+# 数字彩, 每天执行一次
4
+
5
+source /etc/profile
6
+
7
+cd /opt/niucai/crawl
8
+
9
+scrapy crawl lottery >> logs/lottery.log