forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1017 B
Python
35 lines
1017 B
Python
"""Web base loader class."""
|
|
from typing import Any, List
|
|
|
|
import requests
|
|
|
|
from langchain.docstore.document import Document
|
|
from langchain.document_loaders.base import BaseLoader
|
|
|
|
|
|
class WebBaseLoader(BaseLoader):
|
|
"""Loader that uses urllib and beautiful soup to load webpages."""
|
|
|
|
def __init__(self, web_path: str):
|
|
"""Initialize with webpage path."""
|
|
self.web_path = web_path
|
|
|
|
@staticmethod
|
|
def _scrape(url: str) -> Any:
|
|
from bs4 import BeautifulSoup
|
|
|
|
html_doc = requests.get(url)
|
|
soup = BeautifulSoup(html_doc.text, "html.parser")
|
|
return soup
|
|
|
|
def scrape(self) -> Any:
|
|
"""Scrape data from webpage and return it in BeautifulSoup format."""
|
|
return self._scrape(self.web_path)
|
|
|
|
def load(self) -> List[Document]:
|
|
"""Load data into document objects."""
|
|
soup = self.scrape()
|
|
text = soup.get_text()
|
|
metadata = {"source": self.web_path}
|
|
return [Document(page_content=text, metadata=metadata)]
|