[파이썬] requests 라이브러리 소개

The Requests library is a powerful and user-friendly Python package that simplifies the process of sending HTTP requests. It allows you to interact with web services and retrieve data from APIs easily.

Whether you are building a web scraper, accessing RESTful APIs, or making HTTP requests for any other purpose, the Requests library provides a simple and intuitive interface to handle all your HTTP needs.

In this blog post, we will explore the key features of the Requests library and demonstrate how it can be used to make HTTP requests in Python.

Installing the Requests Library

Before getting started, make sure you have the Requests library installed on your system. You can install it using pip, the Python package installer, by running the following command:

pip install requests

Making Basic GET Requests

The Requests library makes it incredibly easy to send GET requests to any URL. Here’s a simple example that retrieves the content of a web page:

import requests

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

print(response.text)

In this example, we import the requests module, use the get() function to send a GET request to the specified URL, and store the response in the response variable. Finally, we print the text attribute of the response, which contains the content of the web page.

Handling Response Status Codes

When making HTTP requests, it’s essential to handle different response status codes properly. The Requests library provides an easy way to access the status code of the response:

import requests

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

if response.status_code == 200:
    print('Request was successful')
else:
    print('Request failed with status code:', response.status_code)

In this example, we check if the response status code is equal to 200, indicating a successful request. If the status code is different, we print an appropriate error message along with the actual status code.

Sending POST Requests

Besides GET requests, you can also send other types of requests, such as POST, PUT, DELETE, etc., using the Requests library. Here’s an example of sending a POST request with some data:

import requests

data = {
    'name': 'John Doe',
    'email': 'johndoe@example.com'
}

response = requests.post('https://www.example.com/api/user', json=data)

print(response.json())

In this example, we define a dictionary data containing some user information. We then send a POST request to an API endpoint with the post() function, passing the data as json. Finally, we print the JSON response returned by the server.

Conclusion

The Requests library is a valuable tool for making HTTP requests in Python. Its simple and intuitive interface makes it easy to interact with web services and retrieve data from APIs.

In this blog post, we covered the basics of using the Requests library, including making GET and POST requests, handling response status codes, and accessing response data. However, Requests offers many more advanced features and options to explore.

Make sure to check out the official Requests documentation for more details and examples: Requests Documentation

Happy coding with the Requests library!