Python3实现自动抽奖

------------恢复内容开始------------

#!/usr/bin/env python3
# _*_coding: utf-8
import os
import yaml
import requests
import time
from time import sleep

current_path = os.path.abspath(os.path.dirname(__file__))  # 获取当前目录的路径
config_dir = current_path + "/choujiang.yml"    # 获取配置文件路径 
def post_http(url, headers, data):    # Post请求方式
    response = requests.post(url=url, headers=headers, data=data)
    print(response)
    print(f"\n响应码: {response.status_code}")
    print(f"\n响应结果: {response.text}")
    return response

def get_http(url, headers,data=None):  # Get请求方式
    response = requests.get(url=url, headers=headers, data=data)
    print(response)
    print(f"\n响应码: {response.status_code}")
#    print(f"\n响应结果: {response.text}")
    return response

def read_yaml():      # 打开读取yaml配置文件内容
    with open(config_dir, r,  encoding="utf-8" ) as f:
        content = f.read()
        contents = yaml.load(content, Loader=yaml.FullLoader)
    return contents

def get_url(api_name):   # 传入url地址
    content=read_yaml()
    new_url =http:// + content[api_name][host] + content[api_name][url]
    print(f"\n请求地址:{new_url}")
    return new_url

def get_headers(api_name):   # 获取请求头
    content = read_yaml()
    new_headers = content[api_name][headers]
    #print(f"\n请求头: {new_headers}")
    return new_headers

def get_data(api_name):  # 获取数据
    content = read_yaml()
    new_data = content[api_name][data]
    return new_data

def get_method(api_name): # 获取请求方法
    content = read_yaml()
    new_method = content[api_name][method]
    return new_method

def get_time():
    # 以毫秒为单位的时间戳
    get_now_milli_time = int(time.time() * 1000)
    print(get_now_milli_time)

def send_request(api_name):   # 发送请求
    try:
        # 获取接口请求参数
        method = get_method(api_name)
        url = get_url(api_name)
        headers = get_headers(api_name)
        # 区分Get和Post方法
        if method == "Get":
            response = get_http(url, headers, data=None)
        else:
            data = get_data(api_name)
            response = post_http(url, headers, data)
        return response
    except Exception as e:
        print(f"\n接口访问出错啦: {e}")

if __name__ == __main__:   # 执行接口
    totalData=send_request(prizes).json()
    print("第一次抽奖")
    counts =totalData[data][counts]
    print(f"总抽奖次数:{counts}")

    num = 1
    while num <= counts:
        num += 1
        print(f"抽奖次数:{num}次")
        data = send_request(draws).json()
        if data[code] == 1003:
            print(f"抽奖次数:{num}次")
            totalData = send_request(prizes).json()
            counts = totalData[data][counts]
            print(f"总抽奖次数:{counts}")

 

choujiang.yml文件用来存放url的数据,一些url中的数据可以通过Charles抓包工具获

Python3实现自动抽奖

 

Python3实现自动抽奖

 

 

 

 

------------恢复内容结束------------

Python3实现自动抽奖

上一篇:leetcode刷题_PYTHON(17):链表(17) 对链表进行插入排序


下一篇:Python的非数字类型