Python调用Bitcoin GitHub API时如何提升X-Ratelimit-limit至5000?
提升GitHub API调用速率限制的方法
Hey there! The reason you're hitting that 60 requests/hour limit is because you're making unauthenticated requests to the GitHub API. To bump that limit up to 5000 requests/hour (the standard for authenticated personal account requests), you just need to add a personal access token (PAT) to your requests. Here's how to fix this:
Step 1: Create a Personal Access Token (PAT)
- Go to your GitHub account settings → Developer settings → Personal access tokens → Tokens (classic)
- Click "Generate new token" (you might need to enter your password)
- For your use case, you only need to check the
public_repopermission (since you're accessing the public bitcoin/bitcoin repo) - Save the generated token somewhere safe—you won't be able to see it again after leaving the page!
Step 2: Modify Your Python Code to Use the Token
You'll need to add an Authorization header to your requests. Here's how to update your existing code to include authentication, plus some improvements to handle pagination and rate limits gracefully:
import urllib.request import json import datetime import codecs import time # Replace this with your generated personal access token GITHUB_TOKEN = "your_personal_access_token_here" # Open output file f = codecs.open("pull6.txt", "w", "utf-8") base_url = "https://api.github.com/repos/bitcoin/bitcoin/pulls?state=closed&labels=bug&page=" # Set up headers with authentication headers = { "Authorization": f"token {GITHUB_TOKEN}", "Accept": "application/vnd.github.v3+json" } page = 1 while True: url = f"{base_url}{page}" req = urllib.request.Request(url, headers=headers) try: response = urllib.request.urlopen(req) # Print rate limit status for monitoring remaining_requests = response.getheader("X-RateLimit-Remaining") reset_timestamp = int(response.getheader("X-RateLimit-Reset")) reset_time = datetime.datetime.fromtimestamp(reset_timestamp) print(f"Fetched page {page} | Remaining requests: {remaining_requests} | Limit resets at: {reset_time}") # Load and process the response data pull_requests = json.load(response) if not pull_requests: # No more data to fetch print("No more pages to fetch. Exiting.") break # Write each PR to the file (adjust format as needed) for pr in pull_requests: f.write(json.dumps(pr, ensure_ascii=False) + "\n") page += 1 time.sleep(1) # Small delay to avoid overwhelming the API except urllib.error.HTTPError as e: if e.code == 403: # Handle rate limit exhaustion reset_timestamp = int(e.headers.get("X-RateLimit-Reset")) wait_seconds = reset_timestamp - time.time() + 10 # Add 10s buffer print(f"Rate limit hit! Waiting {wait_seconds:.2f} seconds until reset...") time.sleep(wait_seconds) else: print(f"Error fetching page {page}: {e}") break f.close()
Key Improvements in This Code:
- Authenticated requests: Uses your PAT to get the 5000 requests/hour limit
- Automatic pagination: Continues fetching pages until there's no more data (no need to hardcode a page range)
- Rate limit handling: If you do hit the limit accidentally, it automatically waits until the reset time
- Status monitoring: Prints real-time info about remaining requests and reset times
If 5000 Requests/Hour Still Isn't Enough:
- GitHub App Authentication: For higher limits (15,000 requests/hour), you can set up a GitHub App instead of a PAT. This is more complex but useful for high-volume use cases.
- GitHub GraphQL API: The GraphQL API lets you fetch more data in a single request (instead of 30 items per REST page), which can drastically reduce the number of requests you need to make.
内容的提问来源于stack exchange,提问作者cumquat8o
相关产品推荐
相关产品推荐

