如何用pygithub按Jupyter Notebook语言过滤GitHub仓库搜索?
Hey there! I’ve dealt with this exact problem before, so let me walk you through fixing it.
Why Your Previous Queries Failed
GitHub’s search engine treats language names with spaces as a single, unified term. When you tried variations like language:JupyterNotebook or language:Jupyter, you weren’t matching the official label GitHub uses for Jupyter Notebook files—"Jupyter Notebook" (with a space included). Without wrapping that spaced name in quotes, GitHub interprets it as separate keywords, which doesn’t return the results you want.
The Correct Query Syntax
To target Jupyter Notebook repositories properly, you need to wrap the full language name in quotes within your search query. This tells GitHub to treat it as a single, exact match.
Updated Code
Here’s how to adjust your PyGitHub script to get the right results:
from github import Github g = Github("login", "password") # Wrap "Jupyter Notebook" in quotes to preserve the space in the language name repositories = g.search_repositories(query='Self organizing map language:"Jupyter Notebook"') for repo in repositories: # Replace with your custom processing logic print(f"Repo: {repo.name} | URL: {repo.html_url}")
Why This Works
- GitHub’s API requires quoted strings for language names that include spaces to ensure exact matches. This is the same rule that applies when using GitHub’s web search bar.
- If you test
language:"Jupyter Notebook"directly on GitHub’s web search, you’ll see it correctly pulls up repositories with.ipynbfiles—this translates perfectly to the PyGitHub query since the library passes your query string straight to GitHub’s backend.
Bonus: Alternative Filter by File Extension
If you want to be even more precise (e.g., find repos with .ipynb files even if Jupyter Notebook isn’t listed as the primary language), you can filter by file extension instead:
repositories = g.search_repositories(query='Self organizing map filename:*.ipynb')
内容的提问来源于stack exchange,提问作者Sergey Vladimirovich

