EN
FRENCH
繁体中文
Beautiful Soup is a Python library used to parse HTML and XML documents for web scraping and data extraction.
It provides a simple interface for navigating page structures, searching for elements, and extracting or modifying content based on tags, attributes, and CSS selectors.
Also known as: BS4, Beautiful Soup 4.
Beautiful Soup and Scrapy are both used in web scraping, but they serve different purposes.
Beautiful Soup is mainly a parsing library. It is lightweight and well suited to small or medium-sized projects where developers need to process HTML or XML content.
Scrapy is a complete web scraping framework that includes crawling, request scheduling, pipelines, middleware, and other tools for building larger scraping systems.
In simple terms:
Beautiful Soup works best with HTML that has already been returned by a server.
Selenium, by contrast, controls a real browser and can interact with JavaScript-powered pages, click buttons, fill forms, scroll, and wait for dynamic content to load.
Beautiful Soup is usually faster and simpler for static pages, while Selenium is more suitable when browser interaction or JavaScript rendering is required.
html.parser and lxml.requests or httpx are typically used to download pages before Beautiful Soup parses them.Beautiful Soup itself does not send web requests or manage proxies.
A common scraping workflow combines Beautiful Soup with an HTTP client such as Python's requests library. The HTTP client retrieves the webpage, while Beautiful Soup parses the returned HTML.
When proxy routing is required, it is normally configured in the HTTP client rather than in Beautiful Soup.
A typical workflow may include:
This separation makes Beautiful Soup easy to integrate into proxy-based scraping workflows.
The following example retrieves a webpage with requests and uses Beautiful Soup to extract article titles:
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
titles = soup.find_all("h2", class_="article-title")
for title in titles:
print(title.get_text(strip=True))
Partenariat