[파이썬] requests와 BeautifulSoup 연동

In this blog post, we will learn how to integrate the requests and BeautifulSoup libraries in Python to scrape and parse web pages efficiently.

Prerequisites

Before we begin, make sure you have requests and BeautifulSoup installed in your Python environment. You can install them using pip:

pip install requests
pip install beautifulsoup4

Making HTTP requests with requests

The requests library is widely used in Python for making HTTP requests. It provides a simple and elegant API for interacting with web servers. Below is an example of how to make a GET request using requests:

import requests

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

if response.status_code == requests.codes.ok:
    print("Request was successful!")
else:
    print("Request failed with status code:", response.status_code)

In the code above, we import the requests library and specify the URL we want to fetch. We then use the get() function to send a GET request to the specified URL. The response from the server is stored in the response object.

Parsing HTML with BeautifulSoup

Once we have obtained the HTML content of a web page, we can use BeautifulSoup to parse and extract the information we need. BeautifulSoup is a powerful library for web scraping and HTML parsing in Python. Below is an example of how to extract the title of a web page using BeautifulSoup:

from bs4 import BeautifulSoup

html_content = response.text

soup = BeautifulSoup(html_content, 'html.parser')
title = soup.title

print("Title:", title.text)

In the code above, we import the BeautifulSoup library and create a BeautifulSoup object by passing in the HTML content and the desired parser (in this case html.parser). We can then use the title attribute of the soup object to retrieve the title tag from the HTML. Finally, we print the text of the title tag.

Conclusion

In this blog post, we learned how to integrate the requests and BeautifulSoup libraries in Python to fetch web pages and extract information from them. The power and simplicity of these libraries make web scraping and HTML parsing tasks much easier. Feel free to explore the documentation of requests and BeautifulSoup to learn more about their functionalities and capabilities.

Happy coding!