[파이썬] requests 응답 텍스트 처리하기

When working with web APIs or making HTTP requests in Python, the requests library is widely used for its simplicity and flexibility. One important aspect of working with requests is handling the response data that we receive. In this blog post, we will explore how to process and manipulate the response text obtained from requests.

Making a Request

Before we dive into processing the response text, let’s quickly go over how to make a request using the requests library. Here is a basic example of making an HTTP GET request:

import requests

# Make a GET request
response = requests.get('https://api.example.com/some-endpoint')

# Process the response
# ...

Processing the Response Text

Once we have made a request and obtained the response, we can access the response text using the text attribute of the response object. The response text typically contains the HTML content, JSON data, or any other text format returned by the server.

Accessing Response Text

Here’s how you can access the response text:

import requests

response = requests.get('https://api.example.com/some-endpoint')

# Access the response text
text_data = response.text

# Print the response text
print(text_data)

Parsing JSON Response

If the response is in JSON format, we can use the json() method provided by the requests library to parse it into a Python dictionary. This makes it easier to work with the response data.

import requests

response = requests.get('https://api.example.com/json-endpoint')

# Parse JSON response into a dictionary
data = response.json()

# Access specific elements in the response data
print(data['key'])

Handling HTML Response

When working with HTML response text, we may need to process and extract specific elements from the HTML. The BeautifulSoup library can be used for HTML parsing and manipulation.

import requests
from bs4 import BeautifulSoup

response = requests.get('https://example.com')

# Create a BeautifulSoup object
soup = BeautifulSoup(response.text, 'html.parser')

# Find specific HTML elements
element = soup.find('tag_name', attrs={'attribute': 'value'})

# Process and manipulate the element
# ...

Data Cleaning and Manipulation

In some cases, we may need to clean or manipulate the response text before using it further. Python provides various string manipulation functions that can help with this.

import requests

response = requests.get('https://api.example.com/some-endpoint')

# Cleaning and manipulation
cleaned_data = response.text.strip()  # Remove leading/trailing whitespaces
formatted_data = cleaned_data.upper()  # Convert to uppercase

# Process and work with the cleaned and formatted data
# ...

Conclusion

Processing and manipulating the response text obtained from requests is an important step when working with web APIs or making HTTP requests in Python. By understanding the content of the response and using appropriate parsing and manipulation techniques, we can effectively extract the required data and perform further operations.

In this blog post, we have explored various techniques for handling response text using the requests library. Whether it’s parsing JSON, processing HTML, or simply cleaning and manipulating the text, Python provides a wide range of tools and libraries to make this task easier.

Happy coding!