Last active
September 21, 2023 11:23
-
-
Save ehzawad/1b58be89e8047a4df8786c12bde62bc2 to your computer and use it in GitHub Desktop.
python md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can use the GitHub API to search for repositories, code, commits, issues, users, topics etc. Here is an example of how to search for repositories using Python's `requests` library. | |
```python | |
import requests | |
import json | |
def search_github(keyword): | |
url = 'https://api.github.com/search/repositories' | |
params = {'q': keyword} | |
response = requests.get(url, params=params) | |
print(response.status_code) | |
print(response.json()) | |
search_github('python') | |
``` | |
This will print out the status code of the response and the JSON response itself, which contains information about the repositories that match the search keyword. | |
Please note that GitHub API has a rate limit. For unauthenticated requests, the rate limit allows for up to 10 requests per minute. If you need to make more requests, you will need to authenticate your request. | |
Here is how you can authenticate your request using a personal access token: | |
```python | |
import requests | |
import json | |
def search_github(keyword, token): | |
url = 'https://api.github.com/search/repositories' | |
params = {'q': keyword} | |
headers = {'Authorization': f'token {token}'} | |
response = requests.get(url, params=params, headers=headers) | |
print(response.status_code) | |
print(response.json()) | |
search_github('python', 'your_personal_access_token') | |
``` | |
Replace `'your_personal_access_token'` with your actual personal access token. You can generate a personal access token in your GitHub account settings. | |
Remember to keep your personal access token secret. Do not share it with anyone and do not include it in your code if you plan to share your code publicly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's how you can modify the function to pretty print the JSON response and extract only the repository name and link:
This will print out the name and the link (html_url) of each repository in the search results. Replace
'your_personal_access_token'
with your actual personal access token.