如何获取今天和本周发布的带有特定标签的问题数量?

How can I fetch the number of questions with a particular tag posted this day and this week?

我想获取带有特定标签的问题的每日和每周计数。

例如,我需要对前 100 种语言或标签提出的问题数量的每日和每周计数:

我找到 an example 个标签的问题总数

  • 求出24小时前和一周前(168小时)的纪元秒。
  • 对于您拥有的每个标签列表,向 /questions 发出两次 GET 请求(以获取今天和本周提出的两个问题)。您应该提供以下参数:
    • filter (see )。您需要在 .wrapper 对象中包含 total 并排除 items.
    • tagged - 你要的标签
    • fromdate - day/week 前的纪元秒
  • 查找返回的 JSON 的 total 属性。它可能与网站上显示的计数不同,因为后者已缓存。

这是 Python 中使用 StackAPI (docs) 的示例:

from datetime import datetime, timedelta
from stackapi import StackAPI

now = datetime.now()
last_day = now - timedelta(days = 1)
last_week = now - timedelta(weeks = 1)

last_day_secs = int(last_day.timestamp())
last_week_secs = int(last_week.timestamp())

# replace accordingly
tags = [ "javascript", "python", "java", "c#" ]

SITE = StackAPI("Whosebug")
# exclude items, include the "total" property of .wrapper
q_filter = "!-)5fGp*dqmLp"

def fetch_q_total(fromdate, tag):
  questions = SITE.fetch("questions",
                         tagged = tag,
                         fromdate = fromdate,
                         filter = q_filter)
  return questions["total"]

for tag in tags:
  q_day_total = fetch_q_total(last_day_secs, tag)
  q_week_total = fetch_q_total(last_week_secs, tag)

  print(f"{q_day_total} {tag} questions were posted in the last 24 hours")
  print(f"{q_week_total} {tag} questions were posted in the last week")