Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet177/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   173   174   175   176   177   178   179   180   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

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.











Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   173   174   175   176   177   178   179   180   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish