Tham số URL API Python

Trong hướng dẫn này, bạn sẽ học cách sử dụng phương thức get của thư viện yêu cầu Python để tìm nạp dữ liệu qua HTTP. Thư viện yêu cầu Python trừu tượng hóa sự phức tạp trong việc thực hiện các yêu cầu HTTP. Phương thức requests.get() cho phép bạn lấy một phản hồi HTTP và phân tích nó theo nhiều cách khác nhau

Đến cuối hướng dẫn này, bạn sẽ học được

  • Phương thức get của Python hoạt động như thế nào
  • Cách tùy chỉnh phương thức nhận yêu cầu Python với các tiêu đề
  • Cách sử dụng các đối tượng phản hồi Python

Mục lục

Hiểu các yêu cầu Python nhận Hàm

Yêu cầu HTTP GET được sử dụng để truy xuất dữ liệu từ tài nguyên được chỉ định, chẳng hạn như trang web. Khi sử dụng thư viện yêu cầu Python, bạn có thể sử dụng hàm .get() để tạo yêu cầu GET cho một tài nguyên cụ thể

Hàm chấp nhận một số tham số khác nhau. Hãy xem xét chức năng và các tham số khác nhau mà nó chấp nhận

# Understand the Python requests.get() Function
import requests
req = requests.get(
   url,
   params=None,
   **kwargs
)

Chúng ta có thể thấy trong khối mã ở trên, hàm chấp nhận hai tham số

  1. Một
    # Making a GET Request with requests.get()
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    print(resp)
    
    # Returns:
    # <Response [200]>
    1, trỏ đến một tài nguyên, và
  2. # Making a GET Request with requests.get()
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    print(resp)
    
    # Returns:
    # <Response [200]>
    2, chấp nhận từ điển hoặc bộ dữ liệu để gửi chuỗi truy vấn

Hàm .get() thực sự là một hàm tiện lợi dựa trên hàm

# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
4. Các đối số từ khóa tùy chọn mà bạn có thể chuyển vào xuất phát từ hàm đó. Hãy xem xét các tùy chọn mà bạn có thể chuyển vào

ParameterDescriptionDefault Value
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
5Whether or not to allow redirection
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
6
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
7A tuple to enable secure HTTP authentication
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
9A string or tuple specifying the certification file or key
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200
1A dictionary of cookies to send to the specified URL
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200
3A dictionary of HTTP headers to send
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200
5A dictionary of the protocol of the proxy URL
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200
7A boolean indicator to determine if . Giá trị mặc định của
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8 có nghĩa là yêu cầu sẽ đợi vô thời hạn.
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
8
# Converting the Response Object to a Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.json())

# Returns: 
# {'page': 1, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 1, 'email': '[email protected]', 'first_name': 'George', 'last_name': 'Bluth', 'avatar': 'https://reqres.in/img/faces/1-image.jpg'}, {'id': 2, 'email': '[email protected]', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': 'https://reqres.in/img/faces/2-image.jpg'}, {'id': 3, 'email': '[email protected]', 'first_name': 'Emma', 'last_name': 'Wong', 'avatar': 'https://reqres.in/img/faces/3-image.jpg'}, {'id': 4, 'email': '[email protected]', 'first_name': 'Eve', 'last_name': 'Holt', 'avatar': 'https://reqres.in/img/faces/4-image.jpg'}, {'id': 5, 'email': '[email protected]', 'first_name': 'Charles', 'last_name': 'Morris', 'avatar': 'https://reqres.in/img/faces/5-image.jpg'}, {'id': 6, 'email': '[email protected]', 'first_name': 'Tracey', 'last_name': 'Ramos', 'avatar': 'https://reqres.in/img/faces/6-image.jpg'}], 'support': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}}
4Một dấu hiệu chuỗi hoặc boolean để kiểm tra chứng chỉ TLS của máy chủ
# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>
6Các đối số từ khóa tùy chọn có sẵn trong hàm requests.get()

Bây giờ bạn đã hiểu rõ về hàm requests.get() của Python, hãy xem cách bạn có thể thực hiện một yêu cầu GET

Tạo một yêu cầu Python NHẬN Yêu cầu

Trong phần này, bạn sẽ học cách sử dụng hàm requests.get() để thực hiện yêu cầu GET. Đối với hướng dẫn này, chúng tôi sẽ sử dụng API mô phỏng cho phép bạn mô phỏng một kịch bản trong thế giới thực. Đối với điều này, chúng tôi sẽ truy vấn điểm cuối

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
0 của URL, bằng cách truy cập URL
# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
1

Hãy xem cách chúng ta có thể sử dụng hàm

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
2 để thực hiện yêu cầu GET

# Making a GET Request with requests.get()
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp)

# Returns:
# <Response [200]>

Hãy chia nhỏ những gì chúng ta đã làm trong đoạn mã trên

  1. Chúng tôi đã nhập thư viện
    # Getting a Response Item from the Dictionary
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    response_dict = resp.json()
    print(response_dict.get('page'))
    
    # Returns: 
    # 1
    3
  2. Chúng tôi đã tạo một biến mới,
    # Getting a Response Item from the Dictionary
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    response_dict = resp.json()
    print(response_dict.get('page'))
    
    # Returns: 
    # 1
    4, là kết quả của việc chuyển điểm cuối của chúng tôi vào hàm
    # Getting a Response Item from the Dictionary
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    response_dict = resp.json()
    print(response_dict.get('page'))
    
    # Returns: 
    # 1
    2
  3. Sau đó, chúng tôi in ra biến, biến này trả về một đối tượng
    # Getting a Response Item from the Dictionary
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    response_dict = resp.json()
    print(response_dict.get('page'))
    
    # Returns: 
    # 1
    6

Trong phần sau, bạn sẽ học cách sử dụng đối tượng

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6 này để truy xuất thông tin phản hồi

Hiểu đối tượng phản hồi yêu cầu Python

Trong phần trước, bạn đã học cách tạo một đối tượng

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6. Đối tượng
# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6 có một số thuộc tính và phương thức khác nhau mà chúng ta có thể truy cập để tìm hiểu thêm về dữ liệu đã được trả về

Một số thuộc tính và phương thức chính có sẵn cho đối tượng

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6 bao gồm

  • # Passing in Sample Headers Into Our Request
    import requests
    headers = {"Authorization": "abc123"}
    resp = requests.get('https://reqres.in/api/users', headers=headers)
    1, là mã số nguyên của trạng thái HTTP được phản hồi, chẳng hạn như 200 hoặc 404
  • # Passing in Sample Headers Into Our Request
    import requests
    headers = {"Authorization": "abc123"}
    resp = requests.get('https://reqres.in/api/users', headers=headers)
    2, trả về
    # Making a GET Request with requests.get()
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    print(resp)
    
    # Returns:
    # <Response [200]>
    6 là mã trạng thái của phản hồi nhỏ hơn 400, nếu không thì
    # Understanding the Response Object
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    print(resp.status_code)
    
    # Returns: 200
    8
  • # Passing in Sample Headers Into Our Request
    import requests
    headers = {"Authorization": "abc123"}
    resp = requests.get('https://reqres.in/api/users', headers=headers)
    5, trả về nội dung được mã hóa JSON của phản hồi
  • # Passing in Sample Headers Into Our Request
    import requests
    headers = {"Authorization": "abc123"}
    resp = requests.get('https://reqres.in/api/users', headers=headers)
    6, chứa nội dung phản hồi
  • # Passing in Sample Headers Into Our Request
    import requests
    headers = {"Authorization": "abc123"}
    resp = requests.get('https://reqres.in/api/users', headers=headers)
    7, trả về yêu cầu cho yêu cầu mới trong chuỗi chuyển hướng, nếu có

Hãy xem cách chúng ta có thể sử dụng một số thuộc tính và phương thức này để hiểu phản hồi mà chúng ta nhận được từ lệnh gọi API của mình

# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200

Trong khối mã ở trên, chúng ta đã kiểm tra xem đối tượng

# Passing in Sample Headers Into Our Request
import requests
headers = {"Authorization": "abc123"}
resp = requests.get('https://reqres.in/api/users', headers=headers)
1 của đối tượng
# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6 là gì. Vì đó là vào những năm 200, chúng tôi có thể tin tưởng rằng một yêu cầu có thể chấp nhận được đã được thực hiện

Hãy xem làm thế nào chúng ta có thể hiểu dữ liệu đã được trả về

# Converting the Response Object to a Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.json())

# Returns: 
# {'page': 1, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 1, 'email': '[email protected]', 'first_name': 'George', 'last_name': 'Bluth', 'avatar': 'https://reqres.in/img/faces/1-image.jpg'}, {'id': 2, 'email': '[email protected]', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': 'https://reqres.in/img/faces/2-image.jpg'}, {'id': 3, 'email': '[email protected]', 'first_name': 'Emma', 'last_name': 'Wong', 'avatar': 'https://reqres.in/img/faces/3-image.jpg'}, {'id': 4, 'email': '[email protected]', 'first_name': 'Eve', 'last_name': 'Holt', 'avatar': 'https://reqres.in/img/faces/4-image.jpg'}, {'id': 5, 'email': '[email protected]', 'first_name': 'Charles', 'last_name': 'Morris', 'avatar': 'https://reqres.in/img/faces/5-image.jpg'}, {'id': 6, 'email': '[email protected]', 'first_name': 'Tracey', 'last_name': 'Ramos', 'avatar': 'https://reqres.in/img/faces/6-image.jpg'}], 'support': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}}

Trong đoạn mã trên, chúng tôi đã chuyển đổi đối tượng Phản hồi của mình thành từ điển Python, bằng cách áp dụng phương thức

# Passing in Sample Headers Into Our Request
import requests
headers = {"Authorization": "abc123"}
resp = requests.get('https://reqres.in/api/users', headers=headers)
5 cho đối tượng của chúng tôi. Do đó, giờ đây chúng tôi có thể truy cập các mục trong phản hồi bằng cách sử dụng khóa của chúng

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1

Trong phần tiếp theo, bạn sẽ tìm hiểu cách chuyển tiêu đề vào yêu cầu GET của Python

Truyền Tiêu đề vào Phương thức yêu cầu Python

Trong nhiều trường hợp, khi truy cập API hoặc dữ liệu web khác, bạn sẽ cần cung cấp một số hình thức xác thực. Điều này có thể được thực hiện bằng cách chuyển thông tin qua

# Understanding the Response Object
import requests

resp = requests.get('https://reqres.in/api/users')
print(resp.status_code)

# Returns: 200
3. Tiêu đề cũng có thể được sử dụng để chuyển các dạng thông tin khác, chẳng hạn như loại nội dung bạn muốn trả về

Hãy xem cách chúng tôi có thể chuyển khóa API giả vào truy vấn của mình

# Passing in Sample Headers Into Our Request
import requests
headers = {"Authorization": "abc123"}
resp = requests.get('https://reqres.in/api/users', headers=headers)

Hãy chia nhỏ đoạn mã trên làm gì

  1. Chúng tôi tạo một từ điển các tiêu đề,
    # Understanding the Response Object
    import requests
    
    resp = requests.get('https://reqres.in/api/users')
    print(resp.status_code)
    
    # Returns: 200
    3, chứa các cặp thông tin khóa-giá trị để chuyển vào yêu cầu của chúng tôi
  2. Chúng tôi sử dụng tham số
    # Passing in Query Parameters Into Our GET Request
    import requests
    params = {'page': '2'}
    resp = requests.get('https://reqres.in/api/users', params=params)
    
    print(resp.content)
    
    # Returns:
    # b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'
    3 để chuyển trong từ điển của chúng tôi vào yêu cầu của chúng tôi

Các loại tiêu đề sẽ được chấp nhận sẽ khác nhau tùy thuộc vào tài nguyên web mà bạn đang truy cập

Trong phần sau, bạn sẽ tìm hiểu cách sử dụng các tham số chuỗi truy vấn trong hàm yêu cầu Python

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
2

Sử dụng tham số chuỗi truy vấn trong phương thức yêu cầu Python

Tham số chuỗi truy vấn cho phép bạn tùy chỉnh yêu cầu GET bằng cách chuyển các giá trị trực tiếp vào URL hoặc vào tham số

# Passing in Query Parameters Into Our GET Request
import requests
params = {'page': '2'}
resp = requests.get('https://reqres.in/api/users', params=params)

print(resp.content)

# Returns:
# b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'
6

Trước khi đi sâu hơn, bạn có thể tự hỏi tại sao bạn muốn làm điều này. Hãy xem dữ liệu đã được trả về bởi lệnh gọi API trước đây của chúng tôi. Dữ liệu chỉ ra rằng sáu bản ghi đã được trả về. Nó cũng chỉ ra rằng trang đầu tiên trong số hai trang đã được trả lại. Vì vậy, làm thế nào để chúng ta truy cập trang thứ hai?

Câu trả lời cho vấn đề này, như bạn có thể đoán, là sử dụng các tham số truy vấn. Để truy cập trang thứ hai trong dữ liệu của chúng tôi, chúng tôi có thể chuyển vào trang mà chúng tôi muốn truy cập trang #2. Có ba cách để thực hiện điều này

  1. Truyền một từ điển các tham số vào tham số
    # Passing in Query Parameters Into Our GET Request
    import requests
    params = {'page': '2'}
    resp = requests.get('https://reqres.in/api/users', params=params)
    
    print(resp.content)
    
    # Returns:
    # b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'
    6, chẳng hạn như
    # Passing in Query Parameters Into Our GET Request
    import requests
    params = {'page': '2'}
    resp = requests.get('https://reqres.in/api/users', params=params)
    
    print(resp.content)
    
    # Returns:
    # b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'
    8
  2. Truyền một danh sách các bộ dữ liệu vào tham số
    # Passing in Query Parameters Into Our GET Request
    import requests
    params = {'page': '2'}
    resp = requests.get('https://reqres.in/api/users', params=params)
    
    print(resp.content)
    
    # Returns:
    # b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'
    6, chẳng hạn như requests.get()0
  3. Trực tiếp sửa đổi chuỗi URL bằng cách nối thêm requests.get()1 vào URL

Hãy xem cách chúng ta có thể sử dụng phương pháp đầu tiên để thực hiện điều này

# Passing in Query Parameters Into Our GET Request
import requests
params = {'page': '2'}
resp = requests.get('https://reqres.in/api/users', params=params)

print(resp.content)

# Returns:
# b'{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}'

Chúng ta có thể thấy điều đó bằng cách chuyển các tham số truy vấn, truy vấn của chúng ta đã trả về trang thứ hai

Phần kết luận

Trong hướng dẫn này, bạn đã học cách thực hiện các yêu cầu GET bằng thư viện Python,

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
3. Lần đầu tiên bạn tìm hiểu cách hoạt động của hàm
# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
2, bao gồm cách truy cập các tham số bổ sung thông qua các đối số từ khóa

Sau đó, bạn đã học cách sử dụng hàm

# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
2 để đưa ra yêu cầu và cách truy cập các thuộc tính cũng như sử dụng các phương thức của đối tượng
# Getting a Response Item from the Dictionary
import requests

resp = requests.get('https://reqres.in/api/users')
response_dict = resp.json()
print(response_dict.get('page'))

# Returns: 
# 1
6 được trả về. Cuối cùng, bạn đã học cách áp dụng tiêu đề và tham số truy vấn cho yêu cầu GET của mình

Làm cách nào để chuyển tham số trong URL API?

Truyền tham số chuỗi truy vấn tới điểm cuối HTTP .
Mở bảng điều khiển Cổng API, sau đó chọn API của bạn
Trong ngăn Tài nguyên, chọn phương thức HTTP được định cấu hình. .
Trong khung Thực thi Phương thức, chọn Yêu cầu Phương thức
Mở rộng danh sách thả xuống Tham số chuỗi truy vấn URL rồi chọn Thêm chuỗi truy vấn

Tham số URL trong API là gì?

Tham số URL (còn được gọi là “chuỗi truy vấn”) là một cách để cấu trúc thông tin bổ sung cho một URL nhất định .

Làm cách nào để đặt tham số đường dẫn trong lệnh gọi API?

Mỗi tham số đường dẫn phải được thay thế bằng một giá trị thực khi khách hàng thực hiện lệnh gọi API. Trong OpenAPI, tham số đường dẫn được xác định bằng cách sử dụng trong. đường dẫn . Tên tham số phải giống như được chỉ định trong đường dẫn. Cũng nhớ thêm yêu cầu. true , vì các tham số đường dẫn luôn được yêu cầu.