Skip to main content
Tech Tutorials & Programming

Published Sep 26, 2025 · Updated Nov 19, 2026

What Is BeautifulSoup and How Do You Use It?

BeautifulSoup is a Python library for parsing HTML and XML. Learn how it works, bs4 vs beautifulsoup4, a step-by-step scraping example, and when to use it.

What Is BeautifulSoup and How Do You Use It?

Key Takeaways

  • Copying data out of raw HTML manually breaks fast, because one missing closing tag can defeat a hand-written pattern.
  • BeautifulSoup parses HTML and XML into a tree you search with Python, using a parser like html.parser or lxml.
  • You install beautifulsoup4, import bs4, and call BeautifulSoup. All three names refer to the same library.
  • BeautifulSoup only parses markup, so on its own it cannot fetch pages, run JavaScript, or crawl many URLs.
  • Pair it with Requests for fetching, Selenium or Playwright for JavaScript pages, and Scrapy for large crawls.
  • Proxidize residential and mobile proxies cover the IP layer when a site varies content by location or limits repeated requests.

Pulling data out of a web page sounds simple until you open the HTML. Titles, prices, and links are wrapped inside nested tags, and a single missing closing tag can break a hand-written regex. Repeat that across a few hundred pages and doing it manually doesn’t look practical anymore.

BeautifulSoup is a Python library built for web scraping. It reads HTML and XML and builds a parse tree. You then find the tags you need with a few lines of code.

This guide explains what BeautifulSoup is and how it works. It walks through a six-step scraping example with Requests and CSV output. It also covers the advantages and limitations of the library, when to choose it, and where proxies fit.

What Is BeautifulSoup?

BeautifulSoup is a Python library that pulls data out of HTML and XML files. It reads a document, builds a parse tree from it, and allows you to search that tree with Python code. It works with a parser of your choice, such as html.parser or lxml. BeautifulSoup only parses markup you give it. It does not download pages, so scrapers pair it with an HTTP library like Requests.

What Does BeautifulSoup Do?

BeautifulSoup finds and extracts data from a page's HTML. Its main jobs are:

  • Search the tree: Find tags by name, attribute, CSS class, text, or CSS selector
  • Navigate the tree: Move between a tag's parents, children, and siblings
  • Extract data: Read the text, attribute values, and links inside the tags you found
  • Modify the tree: Change, add, or remove tags. These edits apply to the copy in memory, not to the live page
  • Parse messy markup: Read invalid HTML through your chosen parser. The tree you get depends on that parser

When to Choose BeautifulSoup for Web Scraping

Choose BeautifulSoup when the data is already in the HTML you download and the main job is parsing it. The following scenarios suit well for web scraping with BeautifulSoup: 

  • Scraping static pages: Product listings, blog archives, or directory pages whose data appears in the page source
  • Running small to mid-sized jobs: A script that checks a few hundred pages each day, such as competitor prices.
  • Parsing saved files: HTML exports, RSS or Atom feeds, and reports stored on disk.
  • Cleaning up messy markup: Older pages with unclosed or nested tags, read through html.parser or lxml.
  • Prototyping selectors: Testing which tags hold your data before you move to a larger framework.
  • Controlling each request yourself: Setting your own headers, sessions, or proxies in Requests, then handing the response to BeautifulSoup.

If the page builds its content with JavaScript, pick Selenium or Playwright. For crawls across thousands of URLs, pick Scrapy.

BeautifulSoup vs beautifulsoup4 vs bs4

BeautifulSoup, beautifulsoup4, and bs4 are three names for the same library, and each one belongs to a different line of your script. You give pip the name beautifulsoup4, you import the module bs4, and you call the class BeautifulSoup. A tutorial that shows from bs4 import BeautifulSoup after pip install beautifulsoup4 uses all three correctly. Writing import beautifulsoup4 raises a ModuleNotFoundError, because no module has that name. 

NameWhat it refers toWhere you use it
BeautifulSoupThe library and its main classsoup = BeautifulSoup(html, "html.parser")
beautifulsoup4The package name on PyPIpip install beautifulsoup4
bs4The Python module you importfrom bs4 import BeautifulSoup

The official documentation warns that a separate package named BeautifulSoup is the older Beautiful Soup 3. Install beautifulsoup4 to get the current version.

How Does BeautifulSoup Work?

BeautifulSoup hands your document to a parser. It then wraps the parser's output in a searchable tree of Python objects. The process has three steps:

  1. You pass BeautifulSoup a string or an open file, and you name a parser
  2. The parser splits the markup into tags and text, and BeautifulSoup builds the parse tree
  3. You search and navigate the tree with methods such as find(), find_all(), and select()

BeautifulSoup supports different parsers and they handle invalid HTML in their own way.

ParserHow you name itWhat to know
html.parser"html.parser"Ships with Python, so it needs no extra install
lxml"lxml"Separate install, and it can also parse XML
html5lib"html5lib"Separate install, and it parses pages the way a browser does

How to Use BeautifulSoup: Step-by-Step Web Scraping Example

To use BeautifulSoup for web scraping, you install it, fetch a page with Requests, parse the HTML, find the elements you want, and save the results. The following example collects book titles, prices, and availability from Books to Scrape, a practice site built for scraping: 

Step 1: Install BeautifulSoup and a Parser

Install BeautifulSoup and Requests with pip. Requests fetches the page, and BeautifulSoup parses it.

bash

The built-in html.parser needs no install. To use lxml instead, add it with pip install lxml.

Step 2: Fetch the Page with Requests

BeautifulSoup does not make HTTP requests, so Requests downloads the page first.

bash

The timeout stops the script from hanging on a slow server. The raise_for_status() call raises an error for 4xx and 5xx responses. Both keep a failed request from reaching the parser.

Step 3: Parse the HTML and Inspect Its Structure

Pass the response body and a parser name to BeautifulSoup. Using response.content hands over the raw bytes, which lets BeautifulSoup detect the encoding.

bash

The prettify() method prints the HTML with indentation. You can also right-click an element in your browser and choose Inspect. On this page, each book has an article tag with the class product_pod.

Step 4: Find Elements with find(), find_all() and CSS Selectors

The find() method returns the first match, or None if nothing matches. The find_all() method returns a list of every match. The select() method does the same job with a CSS selector.

bash

Python reserves the word class, so BeautifulSoup uses class_ as the argument name.

Step 5: Extract Text and Attributes

Use get_text() to read the text inside a tag. Use square brackets to read an attribute such as title or href.

bash

The link text on this site is shortened, so the code reads the full title from the title attribute. The strip=True argument removes extra whitespace around the text.

Step 6: Save the Data to CSV

Write the list of dictionaries to a CSV file with Python's built-in csv module.

bash

The script now produces a books.csv file with one row per book and four columns.

Using BeautifulSoup with Proxies

You set a proxy on the HTTP client that fetches the page, because BeautifulSoup makes no network requests. In the example above, that client is Requests. BeautifulSoup parses whatever HTML the proxied request returns.

bash

Replace the placeholder host, port, and credentials with the details from your proxy provider. If your provider gives you a rotating endpoint, you place it in the same dictionary.

A proxy helps when a site returns different content by region or limits many requests from one IP address. A proxy only changes the IP address the site sees. It leaves cookies, headers, and browser fingerprints as your script sets them. It also does nothing for pages that build their content with JavaScript.

Check the site's terms of service and robots.txt before you scrape it.

Advantages of BeautifulSoup

BeautifulSoup suits static pages and small parsing jobs. It has a short learning curve and a flexible search API. Its main advantages are:

  • Short learning curve: A few methods, such as find(), find_all(), and select(), cover most scraping tasks.
  • Choice of parsers: You can switch between html.parser, lxml, and html5lib without rewriting your search code.
  • Tolerance for messy markup: It reads invalid HTML through the parser you choose.
  • Flexible searching: You can search by tag name, attribute, text, or CSS selector.
  • Easy pairing with other libraries: It takes HTML from Requests and returns plain Python values that you can pass to pandas or the csv module.
  • Simple install: One pip install beautifulsoup4 command sets it up.

Limitations of BeautifulSoup

BeautifulSoup only parses markup. A full scraper needs other tools for fetching pages, rendering JavaScript, and crawling. Its main limitations are:

  • No page fetching: It reads the HTML you supply, so you need Requests or urllib to download it.
  • No JavaScript execution: It sees the HTML as delivered. For pages that build content in the browser, use a browser tool such as Selenium or Playwright.
  • No built-in crawling: It does not follow links, schedule requests, or retry failures. A framework such as Scrapy handles those jobs.
  • Parser-dependent results: Invalid HTML produces different trees under different parsers, so the parser you choose can change what your selectors find.
  • Memory use on large files: It holds the whole parse tree in memory, which becomes a cost on very large documents.
  • No XPath: It searches with its own methods and CSS selectors. For XPath queries, use lxml directly.

Pair BeautifulSoup with the Right Tools for Web Scraping 

BeautifulSoup reads HTML. It does not fetch pages, run JavaScript, or crawl. Match each problem to the tool that solves it:

  • Empty results on a page that shows data in your browser: JavaScript may load it, so add Selenium or Playwright.
  • A crawl across thousands of URLs: Use Scrapy to schedule and retry requests.
  • Different pages by location, or errors after many requests from one IP address: Add a proxy.

Diagnose before you add a proxy. Send a small batch of requests with and without one, and compare the responses. A standard proxy primarily changes the network identity seen by the destination. It does not control cookies, headers, browser state, or render JavaScript.

Proxidize’s residential and mobile proxies cover the IP layer. Residential proxies cover 195+ countries, and mobile proxies offer US city targeting. Run the scraper you built above with Proxidize proxies and compare the pages with what you get today. Try Proxidize with your own targets to collect the web data you are allowed to use. 

Frequently asked questions

Selenium controls a web browser, while BeautifulSoup parses HTML that you already have. Selenium loads pages, runs JavaScript, and can click and type like a user. BeautifulSoup only reads markup, so it cannot render JavaScript or fetch pages itself. Use BeautifulSoup with Requests for static pages. Use Selenium when content appears only after scripts run. You can then pass the rendered page source to BeautifulSoup for its search methods.

BeautifulSoup is a general-purpose parsing library. Whether a scraping project is lawful depends on what you access, how you access it, the data collected, applicable terms, and local law. 

BeautifulSoup4 is widely used, but no dependency is guaranteed safe. Install the exact beautifulsoup4 package, verify its source, and keep it updated. The separate package named BeautifulSoup is the older Beautiful Soup 3, so check the name before you run pip. Treat scraped content as untrusted input, and take care with the files your scraper saves or opens afterward. 

No, BeautifulSoup 4 still gets released. PyPI lists version 4.15.0, released on June 7, 2026. Support for Python 2 ended on December 31, 2020. The last Python 2 release was 4.9.3, so current versions need Python 3. Beautiful Soup 3 is the legacy line, and the official documentation includes a guide for porting code to BS4.

Yes, BeautifulSoup can parse invalid HTML, but the result depends on the parser you choose. The documentation states that different parsers build different trees from the same invalid document. For the broken snippet <a></p>, html.parser returns <a></a>, while lxml wraps it in html and body tags. Name one parser in your code, such as lxml or html5lib, so results stay consistent.

Ready to launch?

Proxies built for real operations.

For teams that depend on stability, not luck.