首页 > 解决方案 > 关于python requests 包的问题

问题描述

所以,我最近开始学习 python 的 requests 包,我遇到了一个挑战。起初我得到了一个链接“<a href="http://pms.zelros.com/" rel="nofollow noreferrer">http://pms.zelros.com/”,它只给了我一个提示:查询参数 id 必须是 uuid v4

我开始研究这个,到目前为止我想出了这个:

'''
def get_optimal_frequency(nb_of_requests=50):
    """
    This sends a number of request in a row to raise Error 429 and get the "optimal frequency" by setting it 
    to the maximal 'X-Rate-Limit-Remaining' that we got + 10% as a margin of error

    :param nb_of_requests: The number of requests sent to raise the Error
    :return: The safe time to wait between requets in ms
    :rtype: int
    """
    session =  requests.Session()
    query = uuid.uuid4()
    optimal_frequency = 0
    headers = {
    'User-Agent': 'Chrome/79.0.3945.88',
    }
    for i  in range(nb_of_requests):
        response = session.get("http://pms.zelros.com", params={'id':query}, headers=headers)
        if response.headers.get('X-Rate-Limit-Remaining') is not None and int(response.headers.get('X-Rate-Limit-Remaining')) > optimal_frequency:   
            optimal_frequency = int(response.headers.get('X-Rate-Limit-Remaining'))
    return 1.1*optimal_frequency


def spam_until_score(score):
    """
    This sends requests with a uuidv4 until the desired score is reached

    :param score: The score wanted
    :return: The response of the last request
    :rtype: requests.models.Response
    """

    start = time.time()
    current_score = 0
    query = uuid.uuid4()
    session =  requests.Session()
    optimal_frequency = get_optimal_frequency()
    headers = {
    'User-Agent': 'Chrome/79.0.3945.88',
    }
    while(current_score < score):
        response = session.get("http://pms.zelros.com", params={'id':query}, headers=headers)
        dict_response = response.json()
        if (int(dict_response.get('score')) < current_score):
            break
        else:
            current_score = int(dict_response.get('score'))
        time.sleep(optimal_frequency/1000)
    end = time.time()
    duration = end - start
    return response, duration

但我被困住了,目标是达到 1 000 000 分,而达到 10 000 分需要 5536 秒。

到目前为止,我得到的帮助是:

10000级

来自/人

让我们添加一个人员有效载荷

“人”:[x,x,x]

2000级

您可以添加分数有效负载以优化您的准备工作

700级

您可以/准备您的请求。

300级

好的开始。很容易 :)

让我们使用一些花哨的 http 动词。

100级

你已经知道你不能向我发送垃圾邮件。

但是您知道与我联系的最佳频率吗?

0级

你好 !

欢迎来到 Zelros 挑战

目标是达到一百万分。

对不起,很长的信息,但这是我的问题:

- 有没有办法在不引发错误 429 的情况下发送更多请求,也许使用并行请求?如果是,我该怎么做?-我真的不明白准备请求对我有什么帮助。- 除了 GET 之外,我还可以使用哪些其他 html 方法?

感谢您的时间和帮助

标签: pythonhtmlpython-requests

解决方案


推荐阅读