天天看點

Python資料分析的過程記錄(六)

Python資料分析的過程記錄(六)

一、需求分析

Python資料分析的過程記錄(六)

①數字1的位置為下期要買小的位置 ,如果中了,下期繼續買中的當期1的新位置

二、代碼:

import requests
import re
import json
import xlwt

TOTAL = 1151


wb = xlwt.Workbook()
# 建立 excel 表格
sh = wb.add_sheet('彩票分析資料處理')
# 建立一個 表單
sh.write(0, 0, "日期")
sh.write(0, 1, "購買數目")
sh.write(0, 2, "命中數目")
sh.write(0, 3, "挂的數目")
sh.write(0, 4, "6次中的數目")
sh.write(0, 5, "6次中的時間")
sh.write(0, 6, "7次中的數目")
sh.write(0, 7, "7次中的時間")
sh.write(0, 8, "8次中的數目")
sh.write(0, 9, "8次中的時間")
sh.write(0, 10, "9次中的數目")
sh.write(0, 11, "9次中的時間")
sh.write(0, 12, "10次中的數目")
sh.write(0, 13, "10次中的時間")
sh.write(0, 14, "11次中的數目")
sh.write(0, 15, "11次中的時間")
sh.write(0, 16, "12次中的數目")
sh.write(0, 17, "12次中的時間")
sh.write(0, 18, "13次中的數目")
sh.write(0, 19, "13次中的時間")
sh.write(0, 20, "14次中的數目")
sh.write(0, 21, "14次中的時間")
# wb.save('test1.xls')

list_of_the_dates = []
for i in range(31):
    list_of_the_dates.append(f"7-{i + 1}")
list_of_the_dates.append("8-01")
list_of_the_dates.append("8-02")

excel_n = 1
for date in list_of_the_dates:
    url = f'https://api.api68.com/pks/getPksHistoryList.do?lotCode=10037&date=2021-0{date}'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62'
    }

    res = requests.get(url, headers=headers)

    # print(res.content.decode())

    new_res = json.loads(res.content.decode())["result"]["data"]

    sh.write(excel_n, 0, new_res[0]["preDrawTime"][:10])

    times_dict = {}
    time_times_dict = {}


    list_of_number = ["01", "02", "03", "04", "05"]
    ln = 0

    success_n = 0

    times_n = 1

    for i in range(1152):
        n1 = 1151 - i
        n2 = 1151 - i - 1
        res_1 = new_res[n1]["preDrawCode"].split(",")
        res_2 = new_res[n2]["preDrawCode"].split(",")

        # index !!!
        position_of_buy = res_1.index(list_of_number[ln])
        # index!!
        # res 1 index -- list_of_number[ln]

        if res_2[position_of_buy] in list_of_number:
            # success
            # ln will not change !!

            success_n += 1

            if f"{times_n}" in time_times_dict.keys():
                time_times_dict[f"{times_n}"].append(new_res[n2]["preDrawTime"][11:])
                times_dict[f"{times_n}"] += 1  # ++
            else:
                time_times_dict[f"{times_n}"] = [new_res[n2]["preDrawTime"][11:]]
                times_dict[f"{times_n}"] = 1  # 1

            times_n = 1  # initial again !!!
            #  1

            # pass
        else:

            times_n += 1
            # times_n += 1

            # change !!
            ln = (ln + 1) % 5
            # change
            # avoid to up error

    sh.write(excel_n, 1, 1151)
    print(success_n)
    sh.write(excel_n, 2, success_n)
    sh.write(excel_n, 3, TOTAL - success_n)
    print(TOTAL - success_n)
    for jn in times_dict.keys():
        if int(jn) >= 6:
            print("times: " + str(times_dict[jn]))
            sh.write(excel_n, 4 + 2 * (int(jn) - 6), times_dict[jn])
            print("time of times: " + str(time_times_dict[jn]))
            sh.write(excel_n, 5 + 2 * (int(jn) - 6), time_times_dict[jn])

    excel_n += 1
wb.save("極速賽車-分析結果.xls")      

三、結果顯示