[Python] How to Retrieve and Check Detailed Response Information with Requests

The Response object returned by executing methods like requests.get() stores all information regarding the response from the server.

In development, understanding the role of each attribute is essential. You need to handle not only the HTML body but also the connection status (status code), metadata (headers), and image data (binary) appropriately.

目次

List of Major Response Attributes

The following are the main properties of the Response object and their uses.

Attribute NameData TypeMeaning / Usage
status_codeintHTTP status code (e.g., 200, 404, 500). Used to judge whether communication was successful.
textstrRetrieves the response body as a string. It is automatically decoded based on the encoding attribute. Used for checking HTML or JSON.
contentbytesRetrieves the response body as bytes (binary). Used when saving images, PDFs, audio files, etc.
encodingstrThe character code used when accessing the text attribute (e.g., 'utf-8', 'ISO-8859-1'). It is estimated automatically, but you can also manually overwrite it.
headersCaseInsensitiveDictResponse header information. It can be handled like a dictionary, but it is a special dictionary that does not distinguish between uppercase and lowercase keys.

Executable Sample Code

The following code is a script that accesses the official Python documentation site and checks each attribute of the obtained response. It includes how to check encoding to prevent garbled text and how to retrieve server information.

import requests

def analyze_response():
    # Target URL (Python Official Documentation)
    target_url = "https://docs.python.org/3/"
    
    print(f"Fetching: {target_url} ...\n")
    
    try:
        # Send request
        response = requests.get(target_url, timeout=10)
        
        # 1. Check status code
        print(f"Status Code: {response.status_code}")
        if response.status_code == 200:
            print(">> Access Successful")
        else:
            print(">> Access Failed")
            return

        # 2. Check encoding information
        # Displays the character code guessed by Requests from headers, etc.
        print(f"Encoding: {response.encoding}")

        # 3. Retrieve header information
        # headers can be accessed like a dictionary
        print("-" * 30)
        content_type = response.headers.get('Content-Type')
        server = response.headers.get('Server', 'Unknown')
        print(f"Content-Type: {content_type}")
        print(f"Server Software: {server}")
        
        # 4. Retrieve content (Text format)
        # Use .text for human-readable data like HTML content
        print("-" * 30)
        print("Response Body (Text - first 100 chars):")
        print(response.text[:100].strip() + "...")
        
        # 5. Retrieve content (Binary format)
        # .content returns bytes. Use this for saving images, etc.
        print("-" * 30)
        print("Response Body (Binary - first 20 bytes):")
        print(response.content[:20])

    except requests.RequestException as e:
        print(f"Communication error occurred: {e}")

if __name__ == "__main__":
    analyze_response()

Explanation: Distinction between text and content

r.text (String)

Use this when handling text data such as web pages (HTML) or API responses (JSON text). The library automatically decodes the data, so you typically use this.

If characters are garbled (displayed incorrectly), you can solve the issue by manually setting the correct character code before accessing r.text, like r.encoding = 'cp932'.

r.content (Bytes)

Use this when downloading non-text data such as image files (.jpg, .png), ZIP files, or PDFs. When writing this to a file, you must open the file in binary mode, such as open('image.jpg', 'wb').

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次