API scraper GitHub: tìm repo và hồ sơ bằng Python
Đội ngũ AxioAPI · Đăng ngày
GitHub chứa dữ liệu công khai tốt nhất về lập trình viên và dự án mã nguồn mở, nhưng REST API chính thức bắt bạn quản lý token và giới hạn theo giờ. API scraper GitHub trả cùng dữ liệu công khai dạng JSON sạch qua một key, phù hợp công cụ nghiên cứu, dashboard tuyển dụng và theo dõi số sao.
Bạn cần gì
- Một API key AxioAPI, lưu trong biến môi trường AXIOAPI_KEY.
- Python 3 và thư viện requests.
- Một từ khóa tìm repository hoặc một username GitHub.
Tìm repository và đọc hồ sơ
GET /api/v1/social/github/repos nhận truy vấn q, sort tùy chọn (stars, forks hoặc updated) và limit. GET /api/v1/social/github/users/{username} trả hồ sơ của một người dùng. Script bên dưới dùng cả hai.
import os
import requests
API = "https://axioapi.com/api/v1/social/github"
HEADERS = {"Authorization": f"Bearer {os.environ['AXIOAPI_KEY']}", "Accept": "application/json"}
def search_repos(query, sort="stars", limit=10):
res = requests.get(f"{API}/repos", headers=HEADERS, timeout=60,
params={"q": query, "sort": sort, "limit": limit})
res.raise_for_status()
return res.json()["data"]["items"]
def user_profile(username):
res = requests.get(f"{API}/users/{username}", headers=HEADERS, timeout=60)
res.raise_for_status()
return res.json()["data"]
if __name__ == "__main__":
for repo in search_repos("web scraping language:python")[:5]:
print(repo["title"], repo["stats"]["likes"], "stars", repo["stats"]["shares"], "forks", repo["extra"]["language"])
profile = user_profile("torvalds")
print(profile["username"], profile["followers"], "followers", profile["posts_count"], "public repos")
Đọc kết quả
- Mỗi repository trong data.items có title (owner/name), text là mô tả, stats.likes là số sao, stats.shares là số fork và extra.language.
- Hồ sơ có username, followers, posts_count (số repository công khai) và liên kết tới hồ sơ.
- Giá trị thiếu được trả về rỗng chứ không bị đoán.
Hoạt động công khai và chi tiết repository
Thêm GET /api/v1/social/github/users/{username}/activity để xem lập trình viên vừa đẩy gì, hoặc GET /api/v1/social/github/repos/{owner}/{repo} để đọc một repository theo lịch và theo dõi số sao theo thời gian.
# Search repositories, sorted by stars
curl -s "https://axioapi.com/api/v1/social/github/repos?q=web%20scraping&sort=stars&limit=5" \
-H "Authorization: Bearer $AXIOAPI_KEY"
# Public activity of a user
curl -s "https://axioapi.com/api/v1/social/github/users/torvalds/activity" \
-H "Authorization: Bearer $AXIOAPI_KEY"
So với GitHub API chính thức
GitHub REST API chính thức cho 60 yêu cầu mỗi giờ khi chưa xác thực và 5.000 yêu cầu mỗi giờ cho mỗi token, và bạn tự tạo, xoay token. Với API này bạn gọi AxioAPI bằng một key và trả theo từng yêu cầu. API chỉ trả dữ liệu công khai, không bao giờ trả repository riêng tư.