WorldQuant--发送post请求时no location的处理办法

当你准备了一堆alpha 通过循环进行simulate的时候,由于登录过期等问题可能会出现如下报错

处理办法就是从中断的地方重新登录,重新开始,下面是一个解决问题的示例代码:

python 复制代码
from time import sleep
import json
import requests
from os.path import expanduser


def login():
    # Load credentials
    with open(expanduser('brain_credentials.txt')) as f:
        credentials = json.load(f)
    # Extract username and password from the list
    username, password = credentials
    # Create a session object
    sess = requests.Session()
    # Set up basic authentication
    sess.auth = HTTPBasicAuth(username, password)
    # Send a POST request to the API for authentication
    response = sess.post('https://api.worldquantbrain.com/authentication')
    # Print response status and content for debugging
    print(response.status_code)
    print(response.text)
    return sess


def main():
    alpha_list = []  # 这里应该是你的 alpha 列表,你需要根据实际情况填充
    sess = login()
    index = 0  # 记录当前处理的 alpha 的索引
    while index < len(alpha_list):
        alpha = alpha_list[index]
        try:
            sim_resp = sess.post(
                "https://api.worldquantbrain.com/simulations",
                json=alpha,
            )
            sim_progress_url = sim_resp.headers.get('Location')
            if sim_progress_url:
                while True:
                    sim_progress_resp = sess.get(sim_progress_url)
                    retry_after_sec = float(sim_progress_resp.headers.get("Retry-After", 0))
                    if retry_after_sec == 0:  # simulation done!
                        break
                    sleep(retry_after_sec)
                alpha_id = sim_progress_resp.json()["alpha"]  # the final simulation result
                print(alpha_id)
            else:
                print("no location, sleep for 10 seconds and try next alpha")
                sleep(10)
        except Exception as e:
            print(f"An error occurred: {e}")
            print("Re-login and try again from the next alpha.")
            sess = login()
            index += 1  # 从下一个 alpha 开始
        index += 1


if __name__ == "__main__":
    main()
相关推荐
做科研的周师兄15 小时前
【机器学习入门】7.4 随机森林:一文吃透随机森林——从原理到核心特点
人工智能·学习·算法·随机森林·机器学习·支持向量机·数据挖掘
TwoAnts&DingJoy19 小时前
数据分析-泊松分布
python·机器学习·数据挖掘·数据分析·统计学·泊松分布
wu_jing_sheng021 小时前
ai 作物分类
人工智能·分类·数据挖掘
api_1800790546021 小时前
异步数据采集实践:用 Python/Node.js 构建高并发淘宝商品 API 调用引擎
大数据·开发语言·数据库·数据挖掘·node.js
csuzhucong1 天前
人类知识体系分类
人工智能·分类·数据挖掘
云端FFF1 天前
论文理解 【LLM-回归】—— Decoding-based Regression
人工智能·数据挖掘·回归
qq_436962181 天前
奥威BI金蝶数据分析可视化方案:200+开箱即用报表驱动智能决策
信息可视化·数据挖掘·数据分析
啦啦啦在冲冲冲2 天前
mse和交叉熵loss,为什么分类问题不用 mse
人工智能·分类·数据挖掘
~~李木子~~2 天前
图像分类项目:Fashion-MNIST 分类(SimpleCNN )
人工智能·分类·数据挖掘
搞科研的小刘选手2 天前
【早稻田大学主办】2026年第三届人工智能与未来教育国际学术会议(AIFE 2026)
人工智能·机器学习·数据挖掘·机器人·未来教育·远程教育·移动学习