如何在PyGithub中使用负限定符筛选GitHub仓库?
在PyGithub中实现GitHub搜索的负向过滤
PyGithub的search_repos方法支持直接传入完整的GitHub搜索查询字符串,和你在网页上用的格式完全一致,不用纠结参数命名问题。直接把包含负向限定符的查询语句作为第一个参数传入即可,完美实现你要的过滤效果。
示例代码
from github import Github # 初始化Github实例,替换为你的个人访问令牌 g = Github("your_personal_access_token") # 直接使用网页搜索的完整查询语句 search_query = "tensorflow language:Python archived:False -topic:tutorial" repos = g.search_repos(search_query) # 遍历输出符合条件的仓库全名 for repo in repos: print(repo.full_name)
动态拼接查询条件
如果需要根据变量动态调整过滤规则,直接拼接字符串就行:
target_keyword = "tensorflow" exclude_topic = "tutorial" search_query = f"{target_keyword} language:Python archived:False -topic:{exclude_topic}"
这种方式完全复用GitHub原生的搜索语法,所有正向、负向限定符都能直接生效,既能在搜索阶段就过滤掉无关仓库,又能节省API速率配额。
内容的提问来源于stack exchange,提问作者akalanka
相关产品推荐
相关产品推荐

