DocsGPT/application/parser/remote/web_loader.py

33 lines
965 B
Python
Raw Normal View History

2023-10-11 21:03:40 +00:00
from application.parser.remote.base import BaseRemote
from langchain_community.document_loaders import WebBaseLoader
2024-05-03 13:17:30 +00:00
headers = {
"User-Agent": "Mozilla/5.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*"
";q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.google.com/",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
}
2023-10-11 21:03:40 +00:00
class WebLoader(BaseRemote):
def __init__(self):
self.loader = WebBaseLoader
2023-10-13 17:52:56 +00:00
def load_data(self, inputs):
urls = inputs
2023-10-12 15:40:23 +00:00
if isinstance(urls, str):
urls = [urls]
2023-10-12 15:40:23 +00:00
documents = []
for url in urls:
try:
2024-05-03 13:17:30 +00:00
loader = self.loader([url], header_template=headers)
2023-10-12 15:40:23 +00:00
documents.extend(loader.load())
except Exception as e:
print(f"Error processing URL {url}: {e}")
continue
return documents