腾讯新闻offset参数

99 阅读1分钟
import urllib.parse
import json
from datetime import datetime, timedelta


def get_offset(last_weight, last_pub_time, last_id, pages=100):
    params_list = []
    current_weight = float(last_weight)
    current_pub_time = datetime.strptime(last_pub_time, "%Y-%m-%d %H:%M:%S")
    current_id = last_id

    for _ in range(pages):
        current_weight -= 0.01  # 权重递减
        current_pub_time -= timedelta(minutes=10)  # 时间递减
        current_id = current_id[:-2] + f"{int(current_id[-2:]) - 1:02d}"  # ID递减

        params = {
            "lastWeight": f"{current_weight:.10f}",
            "lastPubTime": current_pub_time.strftime("%Y-%m-%d %H:%M:%S"),
            "lastID": current_id,
        }
        encoded_params = urllib.parse.quote(json.dumps(params, ensure_ascii=False))
        params_list.append(encoded_params)

    return params_list


last_weight = "5464.8098842593"
last_pub_time = "2024-12-17 11:26:14"
last_id = "20241217A03VH200"
pages = 10

offset_list = get_offset(last_weight, last_pub_time, last_id)
print(offset_list)