GitHub REST API是GitHub提供的一套编程接口,允许开发者通过HTTP请求访问和操作GitHub上的数据。要获取特定城市的GitHub用户,你需要使用GitHub的搜索API。
首先需要获取GitHub API的访问权限:
GitHub的搜索API允许你通过location参数搜索用户:
GET https://api.github.com/search/users?q=location:{城市名}
以下是使用Python实现的完整示例:
import requests
import json
# 替换为你的GitHub个人访问令牌
ACCESS_TOKEN = 'your_personal_access_token'
CITY_NAME = 'Beijing' # 替换为你想搜索的城市
headers = {
'Authorization': f'token {ACCESS_TOKEN}',
'Accept': 'application/vnd.github.v3+json'
}
def get_users_by_location(city):
url = f'https://api.github.com/search/users?q=location:{city}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data['items']
else:
print(f"Error: {response.status_code}")
return None
# 获取用户
users = get_users_by_location(CITY_NAME)
if users:
print(f"Found {len(users)} users in {CITY_NAME}:")
for user in users:
print(f"- {user['login']} ({user['html_url']})")
else:
print("No users found or error occurred.")
GitHub API有分页限制(默认每页30条),如果需要获取更多结果:
def get_users_by_location_with_pagination(city, page=1, per_page=100):
url = f'https://api.github.com/search/users?q=location:{city}&page={page}&per_page={per_page}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()['items']
else:
print(f"Error: {response.status_code}")
return None
通过GitHub API获取城市用户数据是一个强大的工具,但需要注意合理使用,遵守GitHub的使用条款和API限制。
没有搜到相关的文章