रिक्वेस्ट हेडर सेट करें
कई APIs में यूज़र्स को सर्विस के लिए रजिस्टर करके एक API key लेनी पड़ती है. आमतौर पर keys को parameters की बजाय request header में भेजा जाता है.
Yelp API documentation कहता है: "To authenticate API calls with the API Key, set the Authorization HTTP header value as Bearer api_key."
आप एक dictionary सेट करेंगे ताकि यह जानकारी get() को पास हो सके, NYC में सबसे अधिक रेटिंग वाले कैफ़े के लिए API कॉल करेंगे, और response को parse करेंगे.
pandas (संक्षेप में pd) और requests पहले से लोड हैं. API endpoint api_url में है, और key api_key में है. Parameters dictionary params में हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
pandas के साथ सरल Data Ingestion
अभ्यास निर्देश
- एक dictionary
headersबनाइए, जो"Authorization"header वैल्यू के रूप में formatted key स्ट्रिंग पास करे. - आवश्यक headers और parameters के साथ
get()का उपयोग करके Yelp API (api_url) को क्वेरी कीजिए. परिणामresponseके रूप में सेव करें. responseसे JSON डेटा निकालिए. परिणामdataमें सेव करें.dataमें"businesses"वैल्यूज़ को dataframecafesमें लोड कीजिए औरnamesकॉलम को प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Create dictionary that passes Authorization and key string
headers = {____: "Bearer {}".format(____)}
# Query the Yelp API with headers and params set
response = ____
# Extract JSON data from response
data = ____
# Load "businesses" values to a dataframe and print names
cafes = ____
print(cafes.name)