Accessing the Response Content
In order to access the data that we get back in the response, we need to access the content attribute within our requests object:
# accessing the content that we requested from the URL data = r.content print(data)
2www.w3schools.com/tags/ref_httpmessages.asp
Go ahead and run the cell. We’ll get a byte string output with lots of brackets and information in a way that’s difficult to read. Responses from APIs are generally sent in string format, as strings are much lighter data types than objects. The actual response that we get back is in JSON formatting. JavaScript Object Notation (JSON) format is the equivalent of a Python dictionary and is the default format to send data via a request. The next step is to convert the data from a JSON formatted string into a dictionary that we can parse.
Converting the Response
Luckily for us, the requests object comes with a built-in JSON conversion method called json(). After we convert the response to a dictionary, let’s output all the key-value pairs:
# converting data from JSON into a Python dictionary and outputting all key-value pairs data = r.json( ) # converting the data from a string to a dictionary for k, v in data.items( ):
print("Key: { } \t Value: { }".format(k, v)) print(data["name"]) # accessing data directly
Go ahead and run the cell. All the information is now easy to read and access, as seen through the for loop implementation and the simple print statement.
Passing Parameters
Most API calls that you perform will require extra information like parameters or headers. This information is taken in by the API and used to perform a specific task. Let’s perform a call this time while passing parameters in the URL to search for Python- specific repositories on Github:
# outputting specific key-value pairs from data r = requests. get("https://api.github.com/search/repositories?q=language:
python") data = r.json( )
print(data["total_count"]) # output the total number of repositories
that use python
CHapter 10 INtroduCtIoN to data aNalYsIs
Go ahead and run the cell. There are a couple different ways that we can send parameters through the request. In this case, we’ve written them directly into the URL string itself. You may also define them within the get method like the following:
>>> requests.get("https://api.github.com/search/repositories",
>>> params = { 'q' = 'language:python' } )
When sending parameters through the URL, you separate the URL and the parameters with a question mark. To the right of the question mark are a set of key-value pairs that represent the parameters being passed. For our example, the parameter being passed has a key of “q” and a value of “requests+language:python”. The API on Github will take this information and give us back the data on repositories that use Python, because that’s what we asked for in our parameters. Not all APIs require parameters, however, like our first call previously in this lesson. To figure out what is required when calling an API, always read the documentation. Good documentation for APIs is everything and can make your life as a developer much easier.
Note to stop running the virtual environment, simply write into the terminal “deactivate.” You will be asked to activate the environment before each lesson this week.
Do'stlaringiz bilan baham: |