这个代码写的刷票软件,想改成定时运行9次后暂停50秒一直循环下去[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
import time
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import grequests
from fake_useragent import UserAgent
url = 'https://www.baidu.com'
ua = UserAgent()
headers = {
'Tyauthtoken': '4FE86EA2C64241BCA0220AFB0C517C2F',
"User-Agent": ua.random
}
json = {
# 1231231
# 123123
}
def execute_command(num=1):
reqs = [grequests.post(url=url, headers=headers, json=json)
for _ in range(num)]
resps = grequests.map(reqs)
for i, data in enumerate(resps):
content_type = data.headers.get('Content-Type', '').lower()
if data.status_code == 200 and content_type.startswith('application/json'):
json_data = data.json()
print(json_data)
if 'status' in json_data:
status = json_data['status']
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)
SHA_TZ = timezone(
timedelta(hours=8),
name='Asia/Shanghai',
)
beijing_now = utc_now.astimezone(SHA_TZ)
print(beijing_now)
now_fmt = beijing_now.strftime('%H:%M:%S:%f')
print(now_fmt)
if status == 301:
print(f"第{i + 1}个请求失败,状态码为301")
time.sleep(0.1)
elif status == 500:
print(f"第{i + 1}个请求失败,状态码为500")
time.sleep(1)
else:
print('成功')
return
else:
print(f"Error: json 响应里没有 'status' 字段")
else:
print(f"Error: 状态码 {data.status_code}, 响应格式 {content_type}")
def wait_until_8pm():
target_time = datetime.now().replace(hour=7, minute=2, second=59, microsecond=504500)
while True:
now = datetime.now()
if now >= target_time:
break
time.sleep(0.0001)
if __name__ == "__main__":
wait_until_8pm()
start_time = time.perf_counter()
execute_command(10)
end_time = time.perf_counter()
print(f"耗时: {end_time - start_time:.6f} 秒") |