unstructured icon indicating copy to clipboard operation
unstructured copied to clipboard

Replace BeautifulSoup with Scrapling's parser

Open D4Vinci opened this issue 1 year ago • 3 comments

Hi, I like your project, and I use it, but I see you are still using BeautifulSoup. Well, actually, there's a better, faster new parser now: Scrapling.

Scrapling is up to 1735 times faster than BS4 with html5lib and up to 698 times faster than BS4 with Lxml, as benchmarks show below, while using significantly less memory. While analyzing memory usage with memray for another library I use, I noticed that BeautifulSoup consumes more than 7 MB during import alone.

Image

While Scrapling uses 1.5 MB in the same task, plus importing. Scrapling's parser is built on lxml

However, Scrapling doesn't provide only ways to select elements using CSS/XPATH selectors; it also offers new options, such as selecting elements by their text content using lateral search or regular expressions, and a find function similar to BS's, but more powerful and much faster.

Also, Scrapling automatically handles invalid/incomplete HTML through lxml. Here's an article that shows every function in BeautifulSoup with its equivalent in Scrapling to make the migration easy: https://scrapling.readthedocs.io/en/latest/tutorials/migrating_from_beautifulsoup/

Also, if that interests you, it provides a way to make self-healing spiders that adapt to website design changes without AI, and it gives a method for finding elements similar to those found, as the AutoScraper library does. Still, it's way faster and better at this.

Here are two benchmarks from the documentation that compare it to all Python libraries in the market:

Text Extraction Speed Test (5000 nested elements)

# Library Time (ms) vs Scrapling
1 Scrapling 1.92 1.0x
2 Parsel/Scrapy 1.99 1.036x
3 Raw Lxml 2.33 1.214x
4 PyQuery 20.61 ~11x
5 Selectolax 80.65 ~42x
6 BS4 with Lxml 1283.21 ~698x
7 MechanicalSoup 1304.57 ~679x
8 BS4 with html5lib 3331.96 ~1735x

Element Similarity & Text Search Performance

Library Time (ms) vs Scrapling
Scrapling 1.87 1.0x
AutoScraper 10.24 5.476x

It would be a solid addition to unstructured, what do you think?

I'm the author of Scrapling, so you might think I'm biased towards my library, but that's not true —you can see for yourself. If you need any changes to make this happen, please let me know.

D4Vinci avatar Oct 13 '25 18:10 D4Vinci

hey, @D4Vinci thanks for reporting this. I'd love to work on this as a contributor. cc: @PastelStorm

dataCenter430 avatar Mar 24 '26 04:03 dataCenter430

hey, @D4Vinci thanks for reporting this. I'd love to work on this as a contributor. cc: @PastelStorm

Before you start really working on this, do a thorough analysis of how much of a real speedup it will bring to the project. Process a complex HTML document using vanilla unstructured with bs4 10 times, get an average time, then do the same exact workflow with bs4 replaced by Scrapling. If you don't get an order of magnitude difference, it's not worth it.

PastelStorm avatar Mar 24 '26 04:03 PastelStorm

Hi @PastelStorm and @D4Vinci,

We ran a thorough benchmark across representative complex HTML documents (a 3,300+ element multi-page ontology document and an 8,500+ element Wikipedia page, 10 iterations each after discarding warmup runs).

Benchmark Summary

Stage / Workflow Document BS4 Mean (ms) Scrapling / lxml Mean (ms) Speedup
Raw String-to-DOM Parsing complex_ontology.html (3.3k elements) 127.51 ms 7.90 ms 16.15x
Raw String-to-DOM Parsing wikipedia.html (8.5k elements) 308.88 ms 40.69 ms 7.59x
Element Selection (find_all) wikipedia.html 1.90 ms 0.63 ms 3.02x
End-to-End partition_html (v1) wikipedia.html — 153.07 ms Native lxml
End-to-End partition_html (v2) complex_ontology.html 1,728.72 ms ~1,608.00 ms 1.07x (7.4%)

Conclusions:

  1. Vanilla Unstructured v1 Already Runs on lxml: The primary HTML partitioning pipeline (parser.py) does not use BeautifulSoup at all; it was already implemented natively on lxml.etree.HTMLParser with custom class lookups, achieving ~150 ms end-to-end on an 8,500+ DOM element document.
  2. End-to-End Speedup (Amdahl's Law): Where BeautifulSoup is used (the v2 ontology parser), raw parsing constitutes only ~7% of execution time (127 ms out of 1,728 ms). The remaining 93% is consumed by downstream operations (recursive ontology tree building, regex validation, attribute sanitization, hierarchy derivation). Consequently, the overall pipeline speedup is only ~1.07x (7.4%), which falls well below the maintainer's 10x order-of-magnitude requirement.
  3. Compatibility: Scrapling lacks DOM mutation APIs (unwrap(), decompose()) used by Unstructured and enforces HTML5 element closing rules that alter tree structures expected by existing tests.

Given these findings, replacing BeautifulSoup with Scrapling does not yield an order-of-magnitude improvement in the overall document processing workflow.

SK-Sathyavada-008 avatar Sep 25 '26 14:09 SK-Sathyavada-008