-
Notifications
You must be signed in to change notification settings - Fork 44
/
Chapter_14th.py
58 lines (43 loc) · 1.27 KB
/
Chapter_14th.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Example 1
import requests
x = requests.get("https://github.com/")
print(x)
print(x.status_code)
# Output
# <Response [200]>
# 200
# Example 2
import requests
res = requests.get('https://api.github.com/users/sana-rasheed')
print(res)
print(res.status_code)
data = res.json()
print("Followering: ", data['following'])
print("Followers: ", data['followers'])
# Output
# <Response [200]>
# 200
# Followering: 1
# Followers: 30
# Example 3
import requests
url='https://official-joke-api.appspot.com/jokes/programming/random'
res = requests.get(url) # get API end-point response
data = res.json()[0] # convert response text into json format
for key in data:
print(key,":",data[key])
# Output
# id : 74
# type : programming
# setup : Why do C# and Java developers keep breaking their keyboards?
# punchline : Because they use a strongly typed language.
# Example 4
import requests
myobj = [('Ali', 56),('Ahmed', 87),('Amna', 76)]
r = requests.post("http://httpbin.org/post", data=myobj) # post data to API
r # To show API response status
data = r.json() # convert response text into json format
d = data['form'] # Posted data available under form tag
print(d)
# Output
# {'Ahmed': '87', 'Ali': '56', 'Amna': '76'}