How to Scrape Json Text Format using Python

Viewed 35

As i wanted to scrape the data from this website but it gives me an error with no result

 import requests
    import time

headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}

url='https://api.github.com/repositories?since=0'
res = requests.get(url,headers=headers)
data =res.json()
print(data["id"])

print(data["node_id"])
1 Answers

Json data is in an array. So to grab id value you have to iterate over the array.

import requests
   
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
url='https://api.github.com/repositories?since=0'
res = requests.get(url,headers=headers)
data =res.json()

for id in data:
    print(id['id'])

Output:

1
26
27
28
29
31
35
36
42
43
48
52
53
54
56
61
63
65
68
71
73
74
75
92
93
95
98
102
105
118
119
120
127
129
130
131
137
139
140
141
142
144
165
177
179
185
186
190
191
192
193
195
199
203
204
205
206
207
208
211
212
213
217
218
220
230
232
237
245
248
249
251
252
256
257
267
273
279
293
305
307
312
314
319
320
322
324
329
332
334
339
351
360
362
363
364
365
367
368
369
Related