Hướng dẫn python requests default encoding - python yêu cầu mã hóa mặc định

Nếu bạn thực sự thử ví dụ, bạn sẽ tìm thấy:

Show
$ python
Python 3.7.2 (default, Jan 29 2019, 13:41:02) 
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> payload = '''
...  工作报告 
...  总体情况:良好 
... '''
>>> r = requests.post("http://127.0.0.1:8888/post", data=payload)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/venv/lib/python3.7/site-packages/requests/api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/tmp/venv/lib/python3.7/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/tmp/venv/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/tmp/venv/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/tmp/venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/tmp/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/tmp/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/tmp/venv/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/tmp/venv/lib/python3.7/http/client.py", line 1274, in _send_request
    body = _encode(body, 'body')
  File "/tmp/venv/lib/python3.7/http/client.py", line 160, in _encode
    (name.title(), data[err.start:err.end], name)) from None
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 2-5: Body ('工作报告') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

Như được mô tả trong việc phát hiện mã hóa ký tự của một bài đăng HTTP yêu cầu mã hóa mặc định cho bài đăng HTTP là ISO-8859-1 AKA Latin-1. Và khi thông báo lỗi ngay khi kết thúc TraceBack cho bạn biết, bạn có thể buộc nó bằng cách mã hóa thành chuỗi UTF-8

>>> response = requests.get('https://api.github.com')
5; Nhưng tất nhiên, máy chủ của bạn cũng cần phải mong đợi UTF-8; Hoặc bạn sẽ chỉ đơn giản là gửi mojibake Latin-1 vô dụng.

Không có cách nào trong giao diện bài viết để thực thi điều này, nhưng trên thực tế, máy chủ của bạn có thể yêu cầu khách hàng chỉ định rõ ràng mã hóa nội dung của họ bằng cách sử dụng tham số

>>> response = requests.get('https://api.github.com')
6; Có thể trả về một mã lỗi 5xx cụ thể với thông báo lỗi rõ ràng nếu nó bị thiếu.

Một cách ít kỷ luật hơn, bạn có thể có máy chủ của mình cố gắng giải mã các yêu cầu POST đến là UTF-8 và từ chối bài đăng nếu điều đó không thành công.



Các khóa học qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript

« Prev: Python: Các kiểu dữ liệu cơ bản trong Python

  • » Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp
  • Copied !!!
  • Mục lục bài viết:
    • Bắt đầu với các yêu cầu
    • Yêu cầu GET
    • Phản hồi
  • Mã trạng thái
  • Nội dung
  • Tiêu đề
  • Tham số chuỗi truy vấn
  • Yêu cầu tiêu đề
  • Các phương thức HTTP khác
  • Nội dung thư
  • Kiểm tra yêu cầu của bạn
    • Xác thực
    • Đối tượng phiên
    • Số lần thử tối đa
  • Phần kết luận


Bạn đã trải qua một chặng đường dài trong việc tìm hiểu về 

>>> response = requests.get('https://api.github.com')
7thư viện mạnh mẽ của Python .

Bây giờ bạn có thể:

Trong hướng dẫn này, bạn sẽ học cách:

  • Đưa ra yêu cầu bằng các phương thức HTTP phổ biến nhất
  • Tùy chỉnh tiêu đề và dữ liệu yêu cầu của bạn, sử dụng chuỗi truy vấn và nội dung thư
  • Kiểm tra dữ liệu từ các yêu cầu và phản hồi của bạn
  • Thực hiện các yêu cầu đã xác thực
  • Định cấu hình các yêu cầu của bạn để giúp ngăn ứng dụng của bạn sao lưu hoặc làm chậm

Mặc dù tôi đã cố gắng bao gồm nhiều thông tin nhất mà bạn cần để hiểu các tính năng và ví dụ có trong bài viết này, nhưng tôi giả sử có một kiến thức chung rất cơ bản về HTTP . Điều đó nói rằng, bạn vẫn có thể làm theo tốt.

Bây giờ điều đó không còn nữa, hãy đi sâu vào và xem bạn có thể sử dụng như thế nào 

>>> response = requests.get('https://api.github.com')
7trong ứng dụng của mình!

Bắt đầu với >>> response = requests.get('https://api.github.com') 7

Hãy bắt đầu bằng cách cài đặt 

>>> response = requests.get('https://api.github.com')
7thư viện. Để làm như vậy, hãy chạy lệnh sau:

Nếu bạn thích sử dụng Pipenv để quản lý các gói Python , bạn có thể chạy như sau:

$ pipenv install requests

Sau khi 

>>> response = requests.get('https://api.github.com')
7được cài đặt, bạn có thể sử dụng nó trong ứng dụng của mình. Quá trình nhập 
>>> response = requests.get('https://api.github.com')
7trông giống như sau:

Bây giờ bạn đã thiết lập xong, đã đến lúc bắt đầu hành trình của bạn 

>>> response = requests.get('https://api.github.com')
7. Mục tiêu đầu tiên của bạn sẽ là học cách đưa ra 
>>> response.status_code
200
6yêu cầu.

Yêu cầu GET

Các phương thức HTTP chẳng hạn như 

>>> response.status_code
200
6và 
>>> response.status_code
200
8, xác định hành động bạn đang cố gắng thực hiện khi thực hiện một yêu cầu HTTP. Bên cạnh đó 
>>> response.status_code
200
6và 
>>> response.status_code
200
8, có một số phương pháp thông thường khác mà bạn sẽ sử dụng sau này trong hướng dẫn này.

Một trong những phương thức HTTP phổ biến nhất là 

>>> response.status_code
200
6. Các 
>>> response.status_code
200
6phương pháp chỉ ra rằng bạn đang cố gắng để có được hoặc lấy dữ liệu từ một nguồn lực xác định. Để thực hiện một 
>>> response.status_code
200
6yêu cầu, hãy gọi 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
4.

Để kiểm tra điều này, bạn có thể 

>>> response.status_code
200
6yêu cầu API REST gốc của GitHub bằng cách gọi 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6với URL sau:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>

Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của mình. Hãy đi sâu hơn một chút về phản hồi của yêu cầu đó.

Phản hồi

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy thực hiện lại yêu cầu đó một lần nữa, nhưng lần này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:

>>>

>>> response = requests.get('https://api.github.com')

Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của mình. Hãy đi sâu hơn một chút về phản hồi của yêu cầu đó.

Phản hồi

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy thực hiện lại yêu cầu đó một lần nữa, nhưng lần này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:

Trong ví dụ này, bạn đã nắm bắt được giá trị trả về của 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6, là một phiên bản của 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7và lưu trữ nó trong một biến được gọi là 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0. Bây giờ bạn có thể sử dụng 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0để xem rất nhiều thông tin về kết quả của 
>>> response.status_code
200
6yêu cầu của bạn .

Mã trạng thái

>>>

>>> response.status_code
200

Xin chúc mừng! Bạn đã thực hiện yêu cầu đầu tiên của mình. Hãy đi sâu hơn một chút về phản hồi của yêu cầu đó.

Phản hồi

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7là một đối tượng mạnh mẽ để kiểm tra kết quả của yêu cầu. Hãy thực hiện lại yêu cầu đó một lần nữa, nhưng lần này lưu trữ giá trị trả về trong một biến để bạn có thể xem xét kỹ hơn các thuộc tính và hành vi của nó:

Trong ví dụ này, bạn đã nắm bắt được giá trị trả về của 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6, là một phiên bản của 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7và lưu trữ nó trong một biến được gọi là 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0. Bây giờ bạn có thể sử dụng 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0để xem rất nhiều thông tin về kết quả của 
>>> response.status_code
200
6yêu cầu của bạn .

Mã trạng thái

if response:
    print('Success!')
else:
    print('An error has occurred.')

Bit thông tin đầu tiên mà bạn có thể thu thập 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7là mã trạng thái. Mã trạng thái thông báo cho bạn về trạng thái của yêu cầu.

Ví dụ: 

if response:
    print('Success!')
else:
    print('An error has occurred.')
4trạng thái có nghĩa là yêu cầu của bạn đã thành công, trong khi 
if response:
    print('Success!')
else:
    print('An error has occurred.')
5trạng thái có nghĩa là tài nguyên bạn đang tìm kiếm không được tìm thấy. Cũng có nhiều mã trạng thái có thể có khác để cung cấp cho bạn thông tin chi tiết cụ thể về những gì đã xảy ra với yêu cầu của bạn.

Vì vậy, hãy đảm bảo rằng bạn chỉ sử dụng tốc ký thuận tiện này nếu bạn muốn biết yêu cầu nói chung có thành công hay không và sau đó, nếu cần, hãy xử lý phản hồi một cách thích hợp dựa trên mã trạng thái.

Giả sử bạn không muốn kiểm tra mã trạng thái của phản hồi trong một 

import requests
from requests.exceptions import HTTPError

for url in ['https://api.github.com', 'https://api.github.com/invalid']:
    try:
        response = requests.get(url)

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!')
9câu lệnh. Thay vào đó, bạn muốn đưa ra một ngoại lệ nếu yêu cầu không thành công. Bạn có thể làm điều này bằng cách sử dụng 
>>> response = requests.get('https://api.github.com')
>>> response.content
b'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

import requests
from requests.exceptions import HTTPError

for url in ['https://api.github.com', 'https://api.github.com/invalid']:
    try:
        response = requests.get(url)

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!')

Nếu bạn gọi 

>>> response = requests.get('https://api.github.com')
>>> response.content
b'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7, một 
>>> response = requests.get('https://api.github.com')
>>> response.content
b'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
9sẽ được nâng lên cho các mã trạng thái nhất định. Nếu mã trạng thái cho biết một yêu cầu thành công, chương trình sẽ tiếp tục mà không có ngoại lệ nào được nêu ra.

Đọc thêm: Nếu bạn không quen thuộc với chuỗi f của Python 3.6 , tôi khuyến khích bạn tận dụng chúng vì chúng là một cách tuyệt vời để đơn giản hóa các chuỗi được định dạng của bạn.

Bây giờ, bạn đã biết rất nhiều về cách xử lý mã trạng thái của phản hồi mà bạn nhận được từ máy chủ. Tuy nhiên, khi bạn đưa ra một 

>>> response.status_code
200
6yêu cầu, bạn hiếm khi chỉ quan tâm đến mã trạng thái của phản hồi. Thông thường, bạn muốn xem thêm. Tiếp theo, bạn sẽ thấy cách xem dữ liệu thực tế mà máy chủ đã gửi lại trong phần nội dung của phản hồi.

Nội dung

Phản hồi của một 

>>> response.status_code
200
6yêu cầu thường có một số thông tin có giá trị, được gọi là trọng tải, trong nội dung thư. Bằng cách sử dụng các thuộc tính và phương pháp của 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7, bạn có thể xem tải trọng ở nhiều định dạng khác nhau.

Để xem nội dung của phản hồi 

>>> response = requests.get('https://api.github.com')
5, bạn sử dụng 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4:

>>>

>>> response = requests.get('https://api.github.com')
>>> response.content
b'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

>>>

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

>>>

$ pipenv install requests
0

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

>>>

$ pipenv install requests
1

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

Bởi vì việc giải mã 

>>> response = requests.get('https://api.github.com')
5cho một 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
9yêu cầu một lược đồ mã hóa, 
>>> response = requests.get('https://api.github.com')
7sẽ cố gắng đoán mã hóa dựa trên các tiêu đề của phản hồi nếu bạn không chỉ định. Bạn có thể cung cấp mã hóa rõ ràng bằng cách cài đặt 
$ pipenv install requests
01trước khi truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

Nếu bạn nhìn vào phản hồi, bạn sẽ thấy rằng nó thực sự là nội dung JSON được tuần tự hóa. Để lấy từ điển, bạn có thể lấy từ điển mà >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 9bạn đã lấy >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 7và giải mã hóa nó bằng cách sử dụng $ pipenv install requests 05. Tuy nhiên, một cách đơn giản hơn để thực hiện tác vụ này là sử dụng $ pipenv install requests 06:

Các 

$ pipenv install requests
07giá trị trả lại 
$ pipenv install requests
06là một cuốn từ điển, vì vậy bạn có thể truy cập các giá trị trong đối tượng bằng chìa khóa.

>>>

$ pipenv install requests
2

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

>>>

$ pipenv install requests
3

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

>>>

$ pipenv install requests
4

Mặc dù 

>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
4cung cấp cho bạn quyền truy cập vào các byte thô của tải phản hồi, nhưng bạn thường sẽ muốn chuyển đổi chúng thành một chuỗi bằng cách sử dụng mã hóa ký tự như UTF-8 . 
if response:
    print('Success!')
else:
    print('An error has occurred.')
0sẽ làm điều đó cho bạn khi bạn truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

Bởi vì việc giải mã 

>>> response = requests.get('https://api.github.com')
5cho một 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
9yêu cầu một lược đồ mã hóa, 
>>> response = requests.get('https://api.github.com')
7sẽ cố gắng đoán mã hóa dựa trên các tiêu đề của phản hồi nếu bạn không chỉ định. Bạn có thể cung cấp mã hóa rõ ràng bằng cách cài đặt 
$ pipenv install requests
01trước khi truy cập 
>>> response.text
'{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}'
7:

Nếu bạn nhìn vào phản hồi, bạn sẽ thấy rằng nó thực sự là nội dung JSON được tuần tự hóa. Để lấy từ điển, bạn có thể lấy từ điển mà >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 9bạn đã lấy >>> response.text '{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","notifications_url":"https://api.github.com/notifications","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_url":"https://api.github.com/orgs/{org}","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","team_url":"https://api.github.com/teams","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}' 7và giải mã hóa nó bằng cách sử dụng $ pipenv install requests 05. Tuy nhiên, một cách đơn giản hơn để thực hiện tác vụ này là sử dụng $ pipenv install requests 06:

Các 

$ pipenv install requests
07giá trị trả lại 
$ pipenv install requests
06là một cuốn từ điển, vì vậy bạn có thể truy cập các giá trị trong đối tượng bằng chìa khóa.

$ pipenv install requests
5

Bạn có thể làm được nhiều điều với mã trạng thái và nội dung thư. Tuy nhiên, nếu bạn cần thêm thông tin, chẳng hạn như siêu dữ liệu về chính phản hồi, bạn sẽ cần xem xét các tiêu đề của phản hồi.

Tiêu đề

>>>

$ pipenv install requests
6

Bạn thậm chí có thể chuyển các giá trị dưới dạng 

>>> response = requests.get('https://api.github.com')
5:

>>>

$ pipenv install requests
7

Chuỗi truy vấn rất hữu ích cho các 

>>> response.status_code
200
6yêu cầu tham số hóa . Bạn cũng có thể tùy chỉnh các yêu cầu của mình bằng cách thêm hoặc sửa đổi các tiêu đề bạn gửi.

Yêu cầu tiêu đề

Để tùy chỉnh tiêu đề, bạn chuyển một từ điển tiêu đề HTTP để 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6sử dụng 
$ pipenv install requests
28tham số. Ví dụ: bạn có thể thay đổi yêu cầu tìm kiếm trước đó của mình để làm nổi bật các cụm từ tìm kiếm phù hợp trong kết quả bằng cách chỉ định 
$ pipenv install requests
29loại phương tiện trong 
$ pipenv install requests
30tiêu đề:

$ pipenv install requests
8

Các 

$ pipenv install requests
30tiêu đề nói với máy chủ những loại nội dung ứng dụng của bạn có thể xử lý. Trong trường hợp này, vì bạn đang mong đợi các cụm từ tìm kiếm phù hợp được đánh dấu, bạn đang sử dụng giá trị tiêu đề 
$ pipenv install requests
32, là 
$ pipenv install requests
30tiêu đề GitHub độc quyền trong đó nội dung có định dạng JSON đặc biệt.

Trước khi bạn tìm hiểu thêm các cách tùy chỉnh yêu cầu, hãy mở rộng tầm nhìn bằng cách khám phá các phương thức HTTP khác.

Các phương thức HTTP khác

Bên cạnh 

>>> response.status_code
200
6, phương thức HTTP phổ biến khác bao gồm 
>>> response.status_code
200
8, 
$ pipenv install requests
36, 
$ pipenv install requests
37, 
$ pipenv install requests
38, 
$ pipenv install requests
39, và 
$ pipenv install requests
40. 
>>> response = requests.get('https://api.github.com')
7cung cấp một phương thức, với một chữ ký tương tự 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6, cho mỗi phương thức HTTP sau:

>>>

$ pipenv install requests
9

Mỗi lệnh gọi hàm thực hiện một yêu cầu tới 

$ pipenv install requests
43dịch vụ bằng cách sử dụng phương thức HTTP tương ứng. Đối với mỗi phương pháp, bạn có thể kiểm tra phản hồi của họ giống như cách bạn đã làm trước đây:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
0

Tiêu đề, nội dung phản hồi, mã trạng thái và hơn thế nữa được trả về 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
7cho mỗi phương thức. Tiếp theo bạn sẽ có một cái nhìn sâu hơn về các 
>>> response.status_code
200
8, 
$ pipenv install requests
36và 
$ pipenv install requests
39các phương pháp và tìm hiểu cách họ khác với các loại yêu cầu khác.

Nội dung thư

Theo đặc tả HTTP, 

>>> response.status_code
200
8, 
$ pipenv install requests
36, và ít phổ biến 
$ pipenv install requests
39yêu cầu truyền dữ liệu của mình thông qua nội dung thư chứ không phải thông qua các thông số trong chuỗi truy vấn. Khi sử dụng 
>>> response = requests.get('https://api.github.com')
7, bạn sẽ chuyển tải trọng cho 
$ pipenv install requests
52tham số của hàm tương ứng .

$ pipenv install requests
52lấy từ điển, danh sách các bộ giá trị, byte hoặc một đối tượng giống tệp. Bạn sẽ muốn điều chỉnh dữ liệu bạn gửi trong nội dung yêu cầu của mình cho phù hợp với nhu cầu cụ thể của dịch vụ mà bạn đang tương tác.

Ví dụ: nếu loại nội dung yêu cầu của bạn là 

$ pipenv install requests
54, bạn có thể gửi dữ liệu biểu mẫu dưới dạng từ điển:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
1

Bạn cũng có thể gửi cùng một dữ liệu đó dưới dạng danh sách các bộ giá trị:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
2

Tuy nhiên, nếu bạn cần gửi dữ liệu JSON, bạn có thể sử dụng 

$ pipenv install requests
55tham số. Khi bạn chuyển dữ liệu JSON qua 
$ pipenv install requests
55, 
>>> response = requests.get('https://api.github.com')
7sẽ tuần tự hóa dữ liệu của bạn và thêm 
$ pipenv install requests
11tiêu đề chính xác cho bạn.

httpbin.org là một tài nguyên tuyệt vời được tạo ra bởi tác giả của 

>>> response = requests.get('https://api.github.com')
7, Kenneth Reitz . Đó là một dịch vụ chấp nhận các yêu cầu kiểm tra và phản hồi với dữ liệu về các yêu cầu. Ví dụ: bạn có thể sử dụng nó để kiểm tra một 
>>> response.status_code
200
8yêu cầu cơ bản :

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
3

Bạn có thể thấy từ phản hồi rằng máy chủ đã nhận được dữ liệu và tiêu đề yêu cầu của bạn khi bạn gửi chúng. 

>>> response = requests.get('https://api.github.com')
7cũng cung cấp thông tin này cho bạn dưới dạng a 
$ pipenv install requests
62.

Kiểm tra yêu cầu của bạn

Khi bạn thực hiện một yêu cầu, 

>>> response = requests.get('https://api.github.com')
7thư viện sẽ chuẩn bị yêu cầu trước khi thực sự gửi nó đến máy chủ đích. Chuẩn bị yêu cầu bao gồm những thứ như xác thực tiêu đề và tuần tự hóa nội dung JSON.

Bạn có thể xem 

$ pipenv install requests
62bằng cách truy cập 
$ pipenv install requests
65:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
4

Kiểm tra 

$ pipenv install requests
62cung cấp cho bạn quyền truy cập vào tất cả các loại thông tin về yêu cầu đang được thực hiện như trọng tải, URL, tiêu đề, xác thực và hơn thế nữa.

Cho đến nay, bạn đã thực hiện rất nhiều loại yêu cầu khác nhau, nhưng chúng đều có một điểm chung: chúng là những yêu cầu chưa được xác thực đối với các API công khai. Nhiều dịch vụ bạn có thể gặp sẽ muốn bạn xác thực theo một cách nào đó.

Xác thực

Xác thực giúp dịch vụ hiểu bạn là ai. Thông thường, bạn cung cấp thông tin xác thực của mình cho máy chủ bằng cách chuyển dữ liệu qua 

$ pipenv install requests
67tiêu đề hoặc tiêu đề tùy chỉnh do dịch vụ xác định. Tất cả các hàm yêu cầu mà bạn đã thấy cho đến thời điểm này đều cung cấp một tham số được gọi là 
$ pipenv install requests
68, cho phép bạn chuyển thông tin đăng nhập của mình.

Một ví dụ về API yêu cầu xác thực là API người dùng được xác thực của GitHub . Điểm cuối này cung cấp thông tin về hồ sơ của người dùng đã được xác thực. Để thực hiện yêu cầu đối với API người dùng được xác thực, bạn có thể chuyển tên người dùng và mật khẩu GitHub của mình trong một bộ mã tới 

if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
5

Yêu cầu đã thành công nếu thông tin xác thực bạn đã chuyển trong tuple đến 

$ pipenv install requests
68là hợp lệ. Nếu bạn cố gắng thực hiện yêu cầu này mà không có thông tin đăng nhập, bạn sẽ thấy rằng mã trạng thái là 
$ pipenv install requests
71:

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
6

Yêu cầu đã thành công nếu thông tin xác thực bạn đã chuyển trong tuple đến 

$ pipenv install requests
68là hợp lệ. Nếu bạn cố gắng thực hiện yêu cầu này mà không có thông tin đăng nhập, bạn sẽ thấy rằng mã trạng thái là 
$ pipenv install requests
71:

Khi bạn chuyển tên người dùng và mật khẩu của mình trong một bộ vào 

$ pipenv install requests
68tham số, bạn 
>>> response = requests.get('https://api.github.com')
7đang áp dụng thông tin đăng nhập bằng cách sử dụng lược đồ xác thực quyền truy cập Cơ bản của HTTP .

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
7

Yêu cầu đã thành công nếu thông tin xác thực bạn đã chuyển trong tuple đến 

$ pipenv install requests
68là hợp lệ. Nếu bạn cố gắng thực hiện yêu cầu này mà không có thông tin đăng nhập, bạn sẽ thấy rằng mã trạng thái là 
$ pipenv install requests
71:

Khi bạn chuyển tên người dùng và mật khẩu của mình trong một bộ vào 

$ pipenv install requests
68tham số, bạn 
>>> response = requests.get('https://api.github.com')
7đang áp dụng thông tin đăng nhập bằng cách sử dụng lược đồ xác thực quyền truy cập Cơ bản của HTTP .

Do đó, bạn có thể đưa ra yêu cầu tương tự bằng cách chuyển thông tin xác thực Cơ bản rõ ràng bằng cách sử dụng 

$ pipenv install requests
74:

Mặc dù bạn không cần phải xác thực rõ ràng đối với Xác thực cơ bản, nhưng bạn có thể muốn xác thực bằng một phương pháp khác. 

>>> response = requests.get('https://api.github.com')
7cung cấp các phương pháp xác thực khác như 
$ pipenv install requests
76và 
$ pipenv install requests
77.

Bạn thậm chí có thể cung cấp cơ chế xác thực của riêng mình. Để làm như vậy, trước tiên bạn phải tạo một lớp con của 

$ pipenv install requests
78. Sau đó, bạn triển khai 
$ pipenv install requests
79:

>>> requests.get('https://api.github.com')
<Response [200]>
8

Tại đây, $ pipenv install requests 80cơ chế tùy chỉnh của bạn nhận được mã thông báo, sau đó đưa mã thông báo đó vào $ pipenv install requests 81tiêu đề yêu cầu của bạn.

Cơ chế xác thực kém có thể dẫn đến lỗ hổng bảo mật, vì vậy trừ khi một dịch vụ yêu cầu cơ chế xác thực tùy chỉnh vì lý do nào đó, bạn sẽ luôn muốn sử dụng lược đồ xác thực đã thử và đúng như Basic hoặc OAuth.

Trong khi bạn đang suy nghĩ về bảo mật, hãy xem xét xử lý Chứng chỉ SSL bằng cách sử dụng 

>>> response = requests.get('https://api.github.com')
7.

Xác minh chứng chỉ SSL

>>>

>>> requests.get('https://api.github.com')
<Response [200]>
9

Bất kỳ lúc nào dữ liệu bạn đang cố gắng gửi hoặc nhận là nhạy cảm, bảo mật là rất quan trọng. Cách bạn giao tiếp với các trang web an toàn qua HTTP là thiết lập kết nối được mã hóa bằng SSL, có nghĩa là việc xác minh Chứng chỉ SSL của máy chủ mục tiêu là rất quan trọng.

Tin tốt là nó 

>>> response = requests.get('https://api.github.com')
7làm điều này cho bạn theo mặc định. Tuy nhiên, có một số trường hợp bạn có thể muốn thay đổi hành vi này.

Nếu bạn muốn tắt xác minh Chứng chỉ SSL, bạn chuyển import requests from requests.exceptions import HTTPError for url in ['https://api.github.com', 'https://api.github.com/invalid']: try: response = requests.get(url) # If the response was successful, no Exception will be raised response.raise_for_status() except HTTPError as http_err: print(f'HTTP error occurred: {http_err}') # Python 3.6 except Exception as err: print(f'Other error occurred: {err}') # Python 3.6 else: print('Success!') 8đến $ pipenv install requests 85tham số của hàm yêu cầu:

>>> response = requests.get('https://api.github.com')
7 thậm chí cảnh báo bạn khi bạn đưa ra một yêu cầu không an toàn để giúp bạn giữ an toàn cho dữ liệu của mình!

Lưu ý: >>> response = requests.get('https://api.github.com') 7sử dụng một gói được gọi$ pipenv install requests 88 để cung cấp cho Tổ chức phát hành chứng chỉ. Điều này cho >>> response = requests.get('https://api.github.com') 7biết nó có thể tin tưởng vào cơ quan nào. Do đó, bạn nên cập nhật $ pipenv install requests 88thường xuyên để giữ cho các kết nối của mình an toàn nhất có thể.

Hiệu suất

Khi sử dụng 

>>> response = requests.get('https://api.github.com')
7, đặc biệt là trong môi trường ứng dụng sản xuất, điều quan trọng là phải xem xét các tác động về hiệu suất. Các tính năng như kiểm soát thời gian chờ, phiên và giới hạn thử lại có thể giúp bạn giữ cho ứng dụng của mình hoạt động trơn tru.

>>>

>>> response = requests.get('https://api.github.com')
0

Hết giờ

Khi bạn thực hiện một yêu cầu nội tuyến đối với một dịch vụ bên ngoài, hệ thống của bạn sẽ cần phải đợi phản hồi trước khi tiếp tục. Nếu ứng dụng của bạn đợi phản hồi đó quá lâu, các yêu cầu tới dịch vụ của bạn có thể sao lưu, trải nghiệm người dùng của bạn có thể bị ảnh hưởng hoặc các công việc nền của bạn có thể bị treo.

>>>

>>> response = requests.get('https://api.github.com')
1

Theo mặc định, 

>>> response = requests.get('https://api.github.com')
7sẽ đợi phản hồi vô thời hạn, vì vậy bạn hầu như luôn phải chỉ định khoảng thời gian chờ để ngăn những điều này xảy ra. Để đặt thời gian chờ của yêu cầu, hãy sử dụng 
$ pipenv install requests
93tham số. 
$ pipenv install requests
93có thể là số nguyên hoặc số thực thể hiện số giây chờ phản hồi trước khi hết thời gian:

>>> response = requests.get('https://api.github.com')
2

Trong yêu cầu đầu tiên, yêu cầu sẽ hết thời gian chờ sau 1 giây. Trong yêu cầu thứ hai, yêu cầu sẽ hết thời gian chờ sau 3,05 giây.

Đối tượng phiên

Cho đến bây giờ, bạn đã xử lý 

>>> response = requests.get('https://api.github.com')
7các API cấp cao như 
if response.status_code == 200:
    print('Success!')
elif response.status_code == 404:
    print('Not Found.')
6và 
>>> requests.get('https://api.github.com')
<Response [200]>
00. Các hàm này là những phần tóm tắt của những gì đang diễn ra khi bạn thực hiện các yêu cầu của mình. Chúng ẩn các chi tiết triển khai chẳng hạn như cách các kết nối được quản lý để bạn không phải lo lắng về chúng.

Bên dưới những phần trừu tượng đó là một lớp được gọi 

>>> requests.get('https://api.github.com')
<Response [200]>
01. Nếu bạn cần tinh chỉnh quyền kiểm soát của mình đối với cách các yêu cầu đang được thực hiện hoặc cải thiện hiệu suất của các yêu cầu, bạn có thể cần sử dụng 
>>> requests.get('https://api.github.com')
<Response [200]>
01trực tiếp một phiên bản.

Phiên được sử dụng để duy trì các thông số trên các yêu cầu. Ví dụ: nếu bạn muốn sử dụng cùng một xác thực cho nhiều yêu cầu, bạn có thể sử dụng một phiên:

>>> response = requests.get('https://api.github.com')
3

Mỗi khi bạn thực hiện một yêu cầu 

>>> requests.get('https://api.github.com')
<Response [200]>
03, khi nó đã được khởi tạo bằng thông tin xác thực, thông tin xác thực đó sẽ được duy trì.

Việc tối ưu hóa hiệu suất chính của các phiên ở dạng kết nối liên tục. Khi ứng dụng của bạn tạo kết nối với máy chủ bằng cách sử dụng a 

>>> requests.get('https://api.github.com')
<Response [200]>
01, ứng dụng sẽ giữ kết nối đó trong một nhóm kết nối. Khi ứng dụng của bạn muốn kết nối lại với cùng một máy chủ, ứng dụng sẽ sử dụng lại kết nối từ nhóm thay vì thiết lập một kết nối mới.

Số lần thử tối đa

Khi một yêu cầu không thành công, bạn có thể muốn ứng dụng của mình thử lại cùng một yêu cầu. Tuy nhiên, 

>>> response = requests.get('https://api.github.com')
7sẽ không làm điều này cho bạn theo mặc định. Để áp dụng chức năng này, bạn cần triển khai Bộ điều hợp truyền tải tùy chỉnh .

Bộ điều hợp Giao thông cho phép bạn xác định một tập hợp các cấu hình cho mỗi dịch vụ mà bạn đang tương tác. Ví dụ: giả sử bạn muốn tất cả các yêu cầu 

>>> requests.get('https://api.github.com')
<Response [200]>
06thử lại ba lần trước khi cuối cùng tăng a 
>>> requests.get('https://api.github.com')
<Response [200]>
07. Bạn sẽ xây dựng Bộ điều hợp truyền tải, đặt 
>>> requests.get('https://api.github.com')
<Response [200]>
08thông số của nó và gắn nó vào một bộ điều hợp hiện có 
>>> requests.get('https://api.github.com')
<Response [200]>
01:

>>> response = requests.get('https://api.github.com')
4

Khi bạn gắn kết 

>>> requests.get('https://api.github.com')
<Response [200]>
10, 
>>> requests.get('https://api.github.com')
<Response [200]>
11để 
>>> requests.get('https://api.github.com')
<Response [200]>
03, 
>>> requests.get('https://api.github.com')
<Response [200]>
03sẽ tuân thủ cấu hình của nó cho mỗi yêu cầu để https://api.github.com.

Thời gian chờ, Bộ điều hợp truyền tải và phiên là để giữ cho mã của bạn hiệu quả và ứng dụng của bạn có khả năng phục hồi.

Phần kết luận

Bạn đã trải qua một chặng đường dài trong việc tìm hiểu về 

>>> response = requests.get('https://api.github.com')
7thư viện mạnh mẽ của Python .

Bây giờ bạn có thể:

  • Yêu cầu Make sử dụng một loạt các phương pháp HTTP khác nhau như 
    >>> response.status_code
    200
    
    6, 
    >>> response.status_code
    200
    
    8, và
    $ pipenv install requests
    
    36
  • Tùy chỉnh yêu cầu của bạn bằng cách sửa đổi tiêu đề, xác thực, chuỗi truy vấn và nội dung thư
  • Kiểm tra dữ liệu bạn gửi đến máy chủ và dữ liệu máy chủ gửi lại cho bạn
  • Làm việc với xác minh Chứng chỉ SSL
  • Sử dụng 
    >>> response = requests.get('https://api.github.com')
    
    7hiệu quả sử dụng 
    >>> requests.get('https://api.github.com')
    <Response [200]>
    
    08, 
    $ pipenv install requests
    
    93, phiên, và Giao thông vận tải Adapters

Bởi vì bạn đã học cách sử dụng 

>>> response = requests.get('https://api.github.com')
7, bạn được trang bị để khám phá thế giới rộng lớn của các dịch vụ web và xây dựng các ứng dụng tuyệt vời bằng cách sử dụng dữ liệu hấp dẫn mà họ cung cấp.

Hướng dẫn python requests default encoding - python yêu cầu mã hóa mặc định

Các khóa học qua video:
Lập trình C Java C# SQL Server PHP HTML5-CSS3-JavaScript
« Prev: Python: Các kiểu dữ liệu cơ bản trong Python Prev: Python: Các kiểu dữ liệu cơ bản trong Python
» Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp Next: Python: Bố cục PyQt: Tạo các ứng dụng GUI chuyên nghiệp

Copied !!!