
從零開始掌握Reddit獲取API密鑰與數(shù)據(jù)分析
requests
庫進行API調(diào)用。以下是一個示例代碼:
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
response = requests.get(url)
print(response.json())
這個代碼用于獲取GitHub上使用Python語言的項目,并按星級排序。
requests
庫?requests
庫:
pip install requests
安裝后,使用以下代碼驗證安裝:
import requests
print(requests.__version__)
這將打印出requests
庫的版本號,確保其已正確安裝。
json()
方法解析:
response_dict = response.json()
print(response_dict.keys())
這段代碼將響應內(nèi)容轉換為Python字典,并打印出所有的鍵,幫助理解數(shù)據(jù)結構。
headers = {'Authorization': 'token YOUR_ACCESS_TOKEN'}
response = requests.get(url, headers=headers)
在GitHub設置中生成個人AccessToken,并在請求頭中包含它以驗證身份。