English 中文(简体)
Requests - Handling GET Requests
  • 时间:2024-12-22

Requests - Handpng GET Requests


Previous Page Next Page  

This chapter will concentrate more on the GET requests, which is the most common and used very often. The working of GET in the requests module is very easy. Here is a simple example about working with the URL using the GET method.

Example


import requests
getdata = requests.get( https://jsonplaceholder.typicode.com/users )
print(getdata.content)
getdata.content, will print all the data available in the response.   

Output


E:prequests>python makeRequest.py
b [
 {
 "id": 1,
 "name": "Leanne Graham",
 "username": "Bret",

"email": "Sincere@april.biz",
 "address": {
 "street": "Kulas Light
",
 "suite": "Apt. 556",
 "city": "Gwenborough",
 "zipcode": "
92998-3874",
 "geo": {
 "lat": "-37.3159",
 "lng": "81.149
6"
 }
 },
 "phone": "1-770-736-8031 x56442",
 "website": 
"hildegard.org",
 "company": {
 "name": "Romaguera-Crona",
 "catchPhrase": 
"Multi-layered cpent-server neural-net",
 "bs":
"harness real-time e-markets"
 }
 }

You can also pass parameters to the get method using the param attribute as shown below −


import requests
payload = { id : 9,  username :  Delphine }
getdata = requests.get( https://jsonplaceholder.typicode.com/users ,
params=payload)
print(getdata.content)

The details are stored in the object payload in the key/value pair and passed to params, inside get() method.

Output


E:prequests>python makeRequest.py
b [
 {
 "id": 9,
 "name": "Glenna Reichert",
 "username": "Delphine",

 "email": "Chaim_McDermott@dana.io",
 "address": {
 "street":
"Dayna Park",
 "suite": "Suite 449",
 "city": "Bartholomebury",

"zipcode": "76495-3109",
 "geo": {
 "lat": "24.6463",

"lng": "-168.8889"
 }
 },
 "phone": "(775)976-6794 x41206",
 "
website": "conrad.com",
 "company": {
 "name": "Yost and Sons",

"catchPhrase": "Switchable contextually-based project",
 "bs": "aggregate
real-time technologies"
 }
 }
] 
Advertisements