You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python按特定主题或关键词筛选Bitbucket仓库?

如何使用Python按特定主题或关键词筛选Bitbucket仓库?

嘿,我刚好研究过这个问题,给你分享两个实用的方案,不管用atlassian-python-api还是直接调API都能搞定:

方法一:借助atlassian-python-api本地过滤

这个库本身没有直接提供按主题筛选的接口,但我们可以先批量获取仓库列表,再在本地过滤符合条件的仓库,小范围使用完全没问题。

代码示例(主题筛选):

from atlassian import Bitbucket

# 初始化Bitbucket客户端,根据你的环境填对应信息
# 云版请用bitbucket = Bitbucket(cloud=True, username="你的用户名", password="你的应用密码")
bitbucket = Bitbucket(
    url="你的Bitbucket服务器地址",
    username="你的用户名",
    password="你的密码/访问令牌"
)

# 获取所有仓库(云版需要指定workspace参数:bitbucket.get_repos(workspace="你的工作区"))
all_repos = bitbucket.get_all_repos()

# 筛选包含目标主题的仓库
target_topic = "machine-learning"
filtered_repos = [repo for repo in all_repos if target_topic in repo.get("topics", [])]

# 输出结果
for repo in filtered_repos:
    print(f"仓库名称:{repo['name']},所属主题:{repo['topics']}")

如果要按关键词搜索(比如匹配仓库名、描述),只需要调整过滤条件:

target_keyword = "data-processing"
filtered_repos = [
    repo for repo in all_repos
    if target_keyword in repo["name"] or target_keyword in repo.get("description", "")
]

方法二:直接调用Bitbucket REST API(服务器端过滤)

如果你的仓库数量很多,本地过滤效率太低,直接用Bitbucket的REST API做服务器端筛选会更快,API本身支持丰富的搜索参数。

云版Bitbucket代码示例(主题筛选):

import requests

# 配置你的认证信息和工作区
username = "你的用户名"
app_password = "你的应用密码"
workspace = "你的工作区"
target_topic = "machine-learning"

# 构造请求URL和参数
api_url = f"https://api.bitbucket.org/2.0/repositories/{workspace}"
params = {
    "q": f"topics=\"{target_topic}\"",  # 按主题精确匹配
    "pagelen": 100  # 每页返回的仓库数量,可按需调整
}

# 发送请求并处理分页
response = requests.get(api_url, auth=(username, app_password), params=params)
response.raise_for_status()  # 捕获请求错误
filtered_repos = response.json()["values"]

# 处理分页结果
while "next" in response.json():
    response = requests.get(response.json()["next"], auth=(username, app_password))
    response.raise_for_status()
    filtered_repos.extend(response.json()["values"])

# 输出结果
for repo in filtered_repos:
    print(f"仓库名称:{repo['name']},所属主题:{repo.get('topics', [])}")

如果要做模糊关键词搜索,可以调整q参数,比如匹配仓库名包含关键词:

params = {
    "q": f"name~\"{target_keyword}\"",  # ~符号表示模糊匹配
    "pagelen": 100
}

注意事项:

  • 云版Bitbucket必须使用应用密码(App Password)进行认证,不能直接用账号密码,你可以在Bitbucket账户设置里生成。
  • 如果你用的是Bitbucket Data Center/Server版本,API端点和参数会略有不同,比如端点是/rest/api/1.0/repos,过滤参数可以参考对应版本的官方API文档调整。

备注:内容来源于stack exchange,提问作者helium007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:33:08