Skip to content

Commit aa345a4

Browse files
authored
Add get_text_separator parameter to BSHTMLLoader (langchain-ai#3551)
By default get_text doesn't separate content of different HTML tag. Adding option for specifying separator helps with document splitting.
1 parent 568c4f0 commit aa345a4

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

langchain/document_loaders/html_bs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(
1717
file_path: str,
1818
open_encoding: Union[str, None] = None,
1919
bs_kwargs: Union[dict, None] = None,
20+
get_text_separator: str = "",
2021
) -> None:
2122
"""Initialise with path, and optionally, file encoding to use, and any kwargs
2223
to pass to the BeautifulSoup object."""
@@ -33,6 +34,7 @@ def __init__(
3334
if bs_kwargs is None:
3435
bs_kwargs = {"features": "lxml"}
3536
self.bs_kwargs = bs_kwargs
37+
self.get_text_separator = get_text_separator
3638

3739
def load(self) -> List[Document]:
3840
from bs4 import BeautifulSoup
@@ -41,7 +43,7 @@ def load(self) -> List[Document]:
4143
with open(self.file_path, "r", encoding=self.open_encoding) as f:
4244
soup = BeautifulSoup(f, **self.bs_kwargs)
4345

44-
text = soup.get_text()
46+
text = soup.get_text(self.get_text_separator)
4547

4648
if soup.title:
4749
title = str(soup.title.string)

tests/integration_tests/document_loaders/test_bshtml.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
def test_bs_html_loader() -> None:
1010
"""Test unstructured loader."""
1111
file_path = Path(__file__).parent.parent / "examples/example.html"
12-
loader = BSHTMLLoader(str(file_path))
12+
loader = BSHTMLLoader(str(file_path), get_text_separator="|")
1313
docs = loader.load()
1414

1515
assert len(docs) == 1
1616

1717
metadata = docs[0].metadata
18+
content = docs[0].page_content
1819

1920
assert metadata["title"] == "Chew dad's slippers"
2021
assert metadata["source"] == str(file_path)
22+
assert content[:2] == "\n|"
2123

2224

2325
@pytest.mark.skipif(

0 commit comments

Comments
 (0)