排列3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
# https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=30&isVerify=1&pageNo=0
# https://blog.csdn.net/weixin_43509698/article/details/115414628
import csv
import logging
import requests
import json

class PL3_handle:
def __init__(self):
self.urls = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=30&isVerify=1"
self.header = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/84.0.4147.105 Safari/537.36"}
def it_url(self, pages, n):
"""
定义一个生成器,用于生成每页url的data参数
"""
data = {"pageNo": 0}
for i in range(1, pages+1, n):
data['pageNo'] = i
# print(data)
yield data

def page_count(self):
"""子url数"""
try:
url = requests.get(self.urls)
jsdata = json.loads(url.text)
pages = jsdata['value']['pages'] # 206
n = jsdata['value']['pageNo'] # 1
num = jsdata['value']['pageSize']
logging.warning(f"总页数:{pages} 默认页数:{n} 每页数量:{num}")
return pages, n
except Exception as e:
logging.WARNING("请求访问失败", e)
return None

def children_url(self):
"""获取html"""
pages, n = self.page_count()
try:
for dt in self.it_url(pages, n):
# print(dt)
response = requests.get(self.urls, headers=self.header, params=dt)
# print(response.url) #用于校验url是否正确
if response.status_code == requests.codes.ok:
html = response.text
# print(html)
logging.warning(f"第{dt}页请求成功,URL为{response.url}")
self.parsing(html) # 调用解析html方法

except:
logging.warning(f"执行失败!")
return None

def parsing(self,html):
"""用于解析html"""
datas = json.loads(html)
num = len(datas["value"]["list"]) # 统计每页的list数量,这个比较准确 # jsdata['value']['pageSize']
filename = "pl3_date.csv"
# a是追加 w是重写覆盖
with open(filename,"a",newline="") as csvfile:
writer = csv.writer(csvfile)

# writer.writerow(["期号", "开奖日期", "开奖结果","直选注数","直选奖金","直选总奖金","组选3注数","组选3奖金","组选3总奖金","组选6注数","组选6奖金","组选6总奖金","销售额"])
for i in range(num):
期号 = datas["value"]["list"][i]["lotteryDrawNum"]
开奖日期= datas["value"]["list"][i]["lotteryDrawTime"]
开奖结果= datas["value"]["list"][i]["lotteryDrawResult"]

直选注数= datas["value"]["list"][i]["prizeLevelList"][0]["stakeAmount"]
直选奖金= datas["value"]["list"][i]["prizeLevelList"][0]["stakeCount"]
直选总奖金= datas["value"]["list"][i]["prizeLevelList"][0]["totalPrizeamount"]

组选3注数= datas["value"]["list"][i]["prizeLevelList"][1]["stakeAmount"]
组选3奖金= datas["value"]["list"][i]["prizeLevelList"][1]["stakeCount"]
组选3总奖金= datas["value"]["list"][i]["prizeLevelList"][1]["totalPrizeamount"]

组选6注数= datas["value"]["list"][i]["prizeLevelList"][2]["stakeAmount"]
组选6奖金= datas["value"]["list"][i]["prizeLevelList"][2]["stakeCount"]
组选6总奖金= datas["value"]["list"][i]["prizeLevelList"][2]["totalPrizeamount"]

销售额= datas["value"]["list"][i]["totalSaleAmount"]
values = (期号, 开奖日期, 开奖结果,直选注数,直选奖金,直选总奖金,组选3注数,组选3奖金,组选3总奖金,组选6注数,组选6奖金,组选6总奖金,销售额)
writer.writerow(values)


if __name__ == "__main__":
filename = "pl3_date.csv"
# a是追加 w是重写覆盖
with open(filename,"a",newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["期号", "开奖日期", "开奖结果","直选注数","直选奖金","直选总奖金","组选3注数","组选3奖金","组选3总奖金","组选6注数","组选6奖金","组选6总奖金","销售额"])

m = PL3_handle()
m.children_url()