mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-05 19:15:44 +00:00
community[patch]: add BeautifulSoupTransformer remove_unwanted_classnames method (#20467)
Add the remove_unwanted_classnames method to the BeautifulSoupTransformer class, which can filter more effectively. --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
parent
ed26149a29
commit
53bb7dbd29
@ -1,4 +1,4 @@
|
|||||||
from typing import Any, Iterator, List, Sequence, cast
|
from typing import Any, Iterator, List, Sequence, Tuple, Union, cast
|
||||||
|
|
||||||
from langchain_core.documents import BaseDocumentTransformer, Document
|
from langchain_core.documents import BaseDocumentTransformer, Document
|
||||||
|
|
||||||
@ -33,10 +33,11 @@ class BeautifulSoupTransformer(BaseDocumentTransformer):
|
|||||||
def transform_documents(
|
def transform_documents(
|
||||||
self,
|
self,
|
||||||
documents: Sequence[Document],
|
documents: Sequence[Document],
|
||||||
unwanted_tags: List[str] = ["script", "style"],
|
unwanted_tags: Union[List[str], Tuple[str, ...]] = ("script", "style"),
|
||||||
tags_to_extract: List[str] = ["p", "li", "div", "a"],
|
tags_to_extract: Union[List[str], Tuple[str, ...]] = ("p", "li", "div", "a"),
|
||||||
remove_lines: bool = True,
|
remove_lines: bool = True,
|
||||||
*,
|
*,
|
||||||
|
unwanted_classnames: Union[Tuple[str, ...], List[str]] = (),
|
||||||
remove_comments: bool = False,
|
remove_comments: bool = False,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> Sequence[Document]:
|
) -> Sequence[Document]:
|
||||||
@ -48,6 +49,7 @@ class BeautifulSoupTransformer(BaseDocumentTransformer):
|
|||||||
unwanted_tags: A list of tags to be removed from the HTML.
|
unwanted_tags: A list of tags to be removed from the HTML.
|
||||||
tags_to_extract: A list of tags whose content will be extracted.
|
tags_to_extract: A list of tags whose content will be extracted.
|
||||||
remove_lines: If set to True, unnecessary lines will be removed.
|
remove_lines: If set to True, unnecessary lines will be removed.
|
||||||
|
unwanted_classnames: A list of class names to be removed from the HTML
|
||||||
remove_comments: If set to True, comments will be removed.
|
remove_comments: If set to True, comments will be removed.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -56,6 +58,10 @@ class BeautifulSoupTransformer(BaseDocumentTransformer):
|
|||||||
for doc in documents:
|
for doc in documents:
|
||||||
cleaned_content = doc.page_content
|
cleaned_content = doc.page_content
|
||||||
|
|
||||||
|
cleaned_content = self.remove_unwanted_classnames(
|
||||||
|
cleaned_content, unwanted_classnames
|
||||||
|
)
|
||||||
|
|
||||||
cleaned_content = self.remove_unwanted_tags(cleaned_content, unwanted_tags)
|
cleaned_content = self.remove_unwanted_tags(cleaned_content, unwanted_tags)
|
||||||
|
|
||||||
cleaned_content = self.extract_tags(
|
cleaned_content = self.extract_tags(
|
||||||
@ -70,7 +76,31 @@ class BeautifulSoupTransformer(BaseDocumentTransformer):
|
|||||||
return documents
|
return documents
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def remove_unwanted_tags(html_content: str, unwanted_tags: List[str]) -> str:
|
def remove_unwanted_classnames(
|
||||||
|
html_content: str, unwanted_classnames: Union[List[str], Tuple[str, ...]]
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Remove unwanted classname from a given HTML content.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
html_content: The original HTML content string.
|
||||||
|
unwanted_classnames: A list of classnames to be removed from the HTML.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A cleaned HTML string with unwanted classnames removed.
|
||||||
|
"""
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
soup = BeautifulSoup(html_content, "html.parser")
|
||||||
|
for classname in unwanted_classnames:
|
||||||
|
for element in soup.find_all(class_=classname):
|
||||||
|
element.decompose()
|
||||||
|
return str(soup)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def remove_unwanted_tags(
|
||||||
|
html_content: str, unwanted_tags: Union[List[str], Tuple[str, ...]]
|
||||||
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Remove unwanted tags from a given HTML content.
|
Remove unwanted tags from a given HTML content.
|
||||||
|
|
||||||
@ -91,7 +121,10 @@ class BeautifulSoupTransformer(BaseDocumentTransformer):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def extract_tags(
|
def extract_tags(
|
||||||
html_content: str, tags: List[str], *, remove_comments: bool = False
|
html_content: str,
|
||||||
|
tags: Union[List[str], Tuple[str, ...]],
|
||||||
|
*,
|
||||||
|
remove_comments: bool = False,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Extract specific tags from a given HTML content.
|
Extract specific tags from a given HTML content.
|
||||||
|
Loading…
Reference in New Issue
Block a user