2025年12月24日星期三

每日头条自动搜集任务脚本

1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/55RK8C若失效,可用地址

1.购买服务器

阿里云:

服务器购买地址

https://t.aliyun.com/U/55RK8C

若失效,可用地址

https://www.aliyun.com/daily-act/ecs/activity_selection?source=5176.29345612&userCode=49hts92d

腾讯云:

https://curl.qcloud.com/wJpWmSfU

若失效,可用地址

https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console

华为云

https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905

2.部署教程

2024年最新青龙面板跑脚本教程(一)持续更新中

3.代码如下

#!/usr/bin/env python3# -*- coding: utf-8 -*-
"""功能:1) 获取今日头条热榜 Top N(默认 20)2) 调用一言 hitokoto 接口,生成每日心灵鸡汤3) 输出到控制台,并可选保存到文件(txt / md)
依赖:pip install requests
用法:python toutiao_daily.pypython toutiao_daily.py --limit 20 --save mdpython toutiao_daily.py --limit 30 --timeout 10"""
import argparseimport jsonimport sysfrom datetime import datetimefrom typing import AnyDictListOptionalTuple
import requests

TOUTIAO_HOT_URL = "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc"HITOKOTO_URL = "https://v1.hitokoto.cn/"

def safe_get(d: Dict[strAny], keys: List[str], default=None):    """按多个候选字段名依次取值,取到即返回。"""    for k in keys:        if k in d and d[k] is not None:            return d[k]    return default

def fetch_json(url: str, headers: Dict[strstr], timeout: int = 10) -> Dict[strAny]:    """通用:GET 拉取 JSON 并解析。"""    resp = requests.get(url, headers=headers, timeout=timeout)    resp.raise_for_status()    # 有些站点会返回 text/plain 但内容仍是 JSON    try:        return resp.json()    except Exception:        return json.loads(resp.text)

def fetch_toutiao_hot(limit: int = 20, timeout: int = 10) -> List[Dict[strAny]]:    """    获取头条热榜列表,返回标准化结构:    [      {"rank": 1, "title": "...", "hot": "...", "url": "..."},      ...    ]    """    headers = {        "User-Agent": (            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "            "AppleWebKit/537.36 (KHTML, like Gecko) "            "Chrome/122.0.0.0 Safari/537.36"        ),        "Accept""application/json,text/plain,*/*",        "Referer""https://www.toutiao.com/",    }
    data = fetch_json(TOUTIAO_HOT_URL, headers=headers, timeout=timeout)
    # 常见结构:{"data":[...], ...}    items = data.get("data"or data.get("Data"or []    if not isinstance(items, list):        items = []
    results: List[Dict[strAny]] = []    for idx, it in enumerate(items[:limit], start=1):        if not isinstance(it, dict):            continue
        title = safe_get(it, ["Title""title""word""name"], "")        hot = safe_get(it, ["HotValue""hot_value""hotValue""hot""value"], "")        url = safe_get(it, ["Url""url""Link""link""share_url""ShareUrl"], "")
        # 有些字段只给 path,需要补全        if isinstance(url, strand url.startswith("/"):            url = "https://www.toutiao.com" + url
        # 有些返回没有 url,但会有 id / cluster_id,可给一个兜底搜索链接        if not url and title:            url = f"https://www.toutiao.com/search/?keyword={requests.utils.quote(str(title))}"
        results.append(            {                "rank": idx,                "title"str(title).strip(),                "hot"str(hot).strip(),                "url"str(url).strip(),            }        )
    return results

def fetch_hitokoto(timeout: int = 10) -> Tuple[strstr]:    """    调用一言接口,返回 (句子, 来源)    """    headers = {"User-Agent""Mozilla/5.0""Accept""application/json,*/*"}    data = fetch_json(HITOKOTO_URL, headers=headers, timeout=timeout)
    sentence = str(data.get("hitokoto""")).strip()    source = str(data.get("from""")).strip()    return sentence, source

def render_text(hots: List[Dict[strAny]], hitokoto: Tuple[strstr]) -> str:    """渲染为纯文本输出。"""    now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")    lines = []    lines.append(f"今日头条热榜 Top {len(hots)}(生成时间:{now})")    lines.append("=" * 60)
    if not hots:        lines.append("⚠️ 未获取到热榜数据(可能网络受限/被风控/接口变更)。")    else:        for it in hots:            rank = it.get("rank""")            title = it.get("title""")            hot = it.get("hot""")            url = it.get("url""")            hot_part = f" | 热度:{hot}" if hot else ""            lines.append(f"{rank:>2}{title}{hot_part}")            lines.append(f"    {url}")
    lines.append("")    lines.append("每日心灵鸡汤(Hitokoto)")    lines.append("-" * 60)    sentence, source = hitokoto    if sentence:        if source:            lines.append(f"{sentence} —— {source}")        else:            lines.append(sentence)    else:        lines.append("⚠️ 未获取到一言内容。")
    return "\n".join(lines)

def render_markdown(hots: List[Dict[strAny]], hitokoto: Tuple[strstr]) -> str:    """渲染为 Markdown 输出(方便发到公众号/语雀/笔记)。"""    now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")    lines = []    lines.append(f"# 今日头条热榜 Top {len(hots)}")    lines.append(f"> 生成时间:{now}")    lines.append("")
    if not hots:        lines.append("> ⚠️ 未获取到热榜数据(可能网络受限/被风控/接口变更)。")    else:        for it in hots:            rank = it.get("rank""")            title = it.get("title""")            hot = it.get("hot""")            url = it.get("url""")            hot_part = f"(热度:{hot})" if hot else ""            lines.append(f"{rank}. [{title}]({url}{hot_part}")
    lines.append("")    lines.append("## 每日心灵鸡汤(Hitokoto)")    sentence, source = hitokoto    if sentence:        if source:            lines.append(f"> {sentence} —— {source}")        else:            lines.append(f"> {sentence}")    else:        lines.append("> ⚠️ 未获取到一言内容。")
    return "\n".join(lines)

def save_output(content: str, fmt: str) -> str:    """保存到文件并返回文件名。"""    date_str = datetime.now().strftime("%Y%m%d")    suffix = "md" if fmt == "md" else "txt"    filename = f"toutiao_daily_{date_str}.{suffix}"    with open(filename, "w", encoding="utf-8"as f:        f.write(content)    return filename

def main():    parser = argparse.ArgumentParser(description="今日头条热榜 + Hitokoto 每日一句")    parser.add_argument("--limit"type=int, default=20help="热榜条数(默认 20)")    parser.add_argument("--timeout"type=int, default=10help="请求超时时间秒(默认 10)")    parser.add_argument("--save", choices=["none""txt""md"], default="none"help="是否保存文件")    args = parser.parse_args()
    try:        hots = fetch_toutiao_hot(limit=args.limit, timeout=args.timeout)    except Exception as e:        hots = []        print(f"⚠️ 获取头条热榜失败:{e}", file=sys.stderr)
    try:        hitokoto = fetch_hitokoto(timeout=args.timeout)    except Exception as e:        hitokoto = ("""")        print(f"⚠️ 获取一言失败:{e}", file=sys.stderr)
    if args.save == "md":        content = render_markdown(hots, hitokoto)    else:        content = render_text(hots, hitokoto)
    print(content)
    if args.save in ("txt""md"):        filename = save_output(content, args.save)        print(f"\n✅ 已保存:{filename}")

if __name__ == "__main__":    main()
解析

该脚本为每日头条自动搜集任务脚本,主要作用包括:

  1. 抓取今日头条热榜:从头条 PC 热榜接口获取当天热门内容,截取前 20 条,输出"排名 / 标题 / 热度 / 链接"。

  2. 获取每日一句:调用 https://v1.hitokoto.cn/ 获取一句短句,作为"每日心灵鸡汤"。

  3. 汇总输出:把热榜 + 每日一句组合成报告,打印到控制台,并支持可选保存为 TXT/Markdown 文件。

主要方法

  • fetch_toutiao_hot
    负责请求头条热榜接口并解析返回数据,最终把数据整理成统一结构(排名、标题、热度、链接)。同时做了字段兼容与兜底链接处理,尽量避免接口字段变动导致脚本直接挂掉。

  • fetch_hitokoto
    负责调用一言接口并提取句子与来源,供最终展示使用。

  • render_text / render_markdown
    负责把"热榜数据 + 一言句子"格式化成最终输出内容:一个用于纯文本,一个用于 Markdown。

  • save_output
    把最终输出保存到本地文件,文件名按日期自动生成,便于每天归档。

  • main
    负责解析命令行参数、串起整个流程、处理异常(比如接口超时或返回异常),并控制输出/保存。


注意

本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。


历史脚本txt文件获取>>
服务器搭建,人工服务咨询>>

没有评论:

发表评论

天翼云盘任务脚本(2025年12月版)

1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/55RK8C若失效,可用地址 1.购买服务器 阿里云: 服务器购买地址 https : //t.aliyun.com/U/55RK8C 若失效,可用地址 https ://www.aliyun....