github公开项目爬取

py 复制代码
import requests


def search_github_repositories(keyword, token=None, language=None, max_results=1000):
    """
    通过 GitHub API 搜索仓库,支持分页获取所有结果(最多 1000 条)
    :param keyword: 搜索关键词
    :param token: GitHub Token(可选,但建议使用以提高速率限制)
    :param language: 过滤语言
    :param max_results: 最大结果数(GitHub 允许最多 1000 条)
    :return: 仓库列表
    """
    url = "https://api.github.com/search/repositories"
    headers = {"Accept": "application/vnd.github.v3+json"}
    if token:
        headers["Authorization"] = f"Bearer {token}"

    query = keyword
    if language:
        query += f" language:{language}"

    repositories = []
    page = 1
    per_page = 100  # GitHub 单页最大允许 100 条

    while len(repositories) < max_results:
        params = {
            "q": query,
            "sort": "stars",
            "order": "desc",
            "page": page,
            "per_page": per_page
        }

        try:
            response = requests.get(url, headers=headers, params=params)
            response.raise_for_status()
            data = response.json()

            # 检查是否还有更多结果
            if not data.get("items"):
                break

            for item in data["items"]:
                repo_info = {
                    "name": item["name"],
                    "owner": item["owner"]["login"],
                    "url": item["html_url"],
                    "clone_url": item["clone_url"],
                    "description": item["description"],
                    "language": item["language"],
                    "stars": item["stargazers_count"]
                }
                repositories.append(repo_info)
                # 达到用户指定的最大数量时停止
                if len(repositories) >= max_results:
                    break

            page += 1
            # GitHub 最多允许 10 页(即 10 * 100=1000 条)
            if page > 10:
                break

        except requests.exceptions.RequestException as e:
            print(f"请求失败: {e}")
            break

    return repositories


def save_to_txt(results, filename="github_results.txt"):
    """
       将结果保存到文本文件
       :param results: 仓库列表
       :param filename: 保存文件名
       """
    with open(filename, "w", encoding="utf-8") as f:
        for repo in results:
            # 格式化单行文本(用 | 分隔关键信息)
            line = (
                f"仓库: {repo['owner']}/{repo['name']} | "
                f"URL: {repo['url']} | "
                f"语言: {repo['language']} | "
                f"星数: {repo['stars']} | "
                f"克隆地址: {repo['clone_url']}\n"
            )
            f.write(line)
    print(f"结果已保存至 {filename}")


# 示例用法
if __name__ == "__main__":
    keyword = "Aerospace Control"
    language = "Python"
    token = "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"  # 强烈建议使用 Token

    # 获取最多 1000 条结果(实际数量取决于搜索匹配的总数)
    results = search_github_repositories(keyword, token, language=language, max_results=1000)
    if results:
        print(f"找到 {len(results)} 个 {language} 相关仓库:")
        for idx, repo in enumerate(results, 1):
            print(f"\n{idx}. {repo['owner']}/{repo['name']}")
            print(f"   URL: {repo['url']}")
            print(f"   语言: {repo['language']}")  # 显示语言
            print(f"   克隆地址: {repo['clone_url']}")
    else:
        print("未找到结果")
    if results:
        save_to_txt(results)
        print(f"实际获取 {len(results)} 条结果")
    else:
        print("未找到结果")
相关推荐
阿里嘎多学长12 小时前
2026-07-10 GitHub 热点项目精选
开发语言·程序员·github·代码托管
今夕资源网19 小时前
AI声音克隆软件 CosyVoice今夕一键整合包解压即用 阿里巴巴通义实验室开源 github斩获22K星标
人工智能·github·多国语言·声音克隆·零样本语音克隆·感情·ai语音克隆
胡萝卜术21 小时前
从聊天模型到本地执行助手:Remote MCP 多工具 Agent 实战
面试·架构·github
慕容引刀1 天前
告别Commit信息纠结:使用Git AI Commit插件实现规范化提交
人工智能·git·github·visual studio code·visual studio
GoGeekBaird1 天前
我开源了 BeeWeave,给 AI Agent 搭一个越用越懂你的知识创作台
后端·github
fthux1 天前
GitZip Pro 源码解析:一个 GitHub 文件/文件夹下载扩展是如何工作的(一)整体架构与扩展入口
人工智能·ai·开源·github·open source
第一程序员1 天前
Rust Agent 子进程执行:Command 之前,先定义输入和超时
python·rust·github
2501_936960362 天前
GitHub初步了解
github
wangruofeng2 天前
11 万 Star 的生成式 AI 入门课,Microsoft 做对了什么
github·aigc·ai编程
IOT那些事儿2 天前
GitHub Profile 429: Too Many Requests
github·profile·429