Skip to content

Python scraper based on AI

Merge branch '88-blockscraper-implementation' into main

Marco Vinciguerracffcf80a75c1
diff --cc pyproject.toml
index d862966e,9b358ae5..5846e7c6
--- a/pyproject.toml
+++ b/pyproject.toml
@@@ -61,22 -21,39 +61,24 @@@ classifiers =
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
+requires-python = ">= 3.9, < 3.12"
+
++
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
+
++
+[tool.rye]
+managed = true
+dev-dependencies = [
+ "pytest==8.0.0",
+ "pytest-mock==3.14.0"
+]
-[tool.poetry.dependencies]
-python = "^3.9"
-langchain = "0.1.14"
-langchain-openai = "0.1.1"
-langchain-google-genai = "1.0.1"
-html2text = "2020.1.16"
-faiss-cpu = "1.8.0"
-beautifulsoup4 = "4.12.3"
-pandas = "2.0.3"
-python-dotenv = "1.0.1"
-tiktoken = {version = ">=0.5.2,<0.6.0"}
-tqdm = "4.66.1"
-google = "3.0.0"
-minify-html = "0.15.0"
-free-proxy = "1.1.1"
-langchain-groq = "0.1.3"
-playwright = "^1.43.0"
-langchain-aws = "^0.1.2"
-langchain-anthropic = "^0.1.11"
-yahoo-search-py="^0.3"
-
-[tool.poetry.dev-dependencies]
-pytest = "8.0.0"
-
-[tool.poetry.group.docs]
+[tool.rye.group.docs]
optional = true
-[tool.poetry.group.docs.dependencies]
+[tool.rye.group.docs.dependencies]
sphinx = "7.1.2"
sphinx-rtd-theme = "2.0.0"
-
-[build-system]
-requires = ["poetry-core>=1.0.0"]
-build-backend = "poetry.core.masonry.api"
-
diff --cc scrapegraphai/nodes/fetch_node.py
index 6528f098,4cd4f64e..0bfb0111
--- a/scrapegraphai/nodes/fetch_node.py
+++ b/scrapegraphai/nodes/fetch_node.py
@@@ -82,84 -61,33 +82,85 @@@ class FetchNode(BaseNode)
input_data = [state[key] for key in input_keys]
source = input_data[0]
- if self.input == "json_dir" or self.input == "xml_dir":
- compressed_document = [Document(page_content=source, metadata={
- "source": "local_dir"
- })]
- # if it is a local directory
+ if (
+ input_keys[0] == "json_dir"
+ or input_keys[0] == "xml_dir"
+ or input_keys[0] == "csv_dir"
+ ):
+ compressed_document = [
+ Document(page_content=source, metadata={"source": "local_dir"})
+ ]
+ state.update({self.output[0]: compressed_document})
+ return state
+
+ # handling for pdf
+ elif input_keys[0] == "pdf":
+ loader = PyPDFLoader(source)
+ compressed_document = loader.load()
+ state.update({self.output[0]: compressed_document})
+ return state
+
+ elif input_keys[0] == "csv":
+ compressed_document = [
+ Document(
+ page_content=str(pd.read_csv(source)), metadata={"source": "csv"}
+ )
+ ]
+ state.update({self.output[0]: compressed_document})
+ return state
+
+ elif input_keys[0] == "json":
+ f = open(source)
+ compressed_document = [
+ Document(page_content=str(json.load(f)), metadata={"source": "json"})
+ ]
+ state.update({self.output[0]: compressed_document})
+ return state
+
+ elif input_keys[0] == "xml":
+ with open(source, "r", encoding="utf-8") as f:
+ data = f.read()
+ compressed_document = [
+ Document(page_content=data, metadata={"source": "xml"})
+ ]
+ state.update({self.output[0]: compressed_document})
+ return state
+
+ elif self.input == "pdf_dir":
+ pass
+
elif not source.startswith("http"):
- compressed_document = [Document(page_content=remover(source), metadata={
- "source": "local_dir"
- })]
+ title, minimized_body, link_urls, image_urls = cleanup_html(source, source)
+ parsed_content = f"Title: {title}, Body: {minimized_body}, Links: {link_urls}, Images: {image_urls}"
+ compressed_document = [Document(page_content=parsed_content,
+ metadata={"source": "local_dir"}
+ )]
+
+ elif self.useSoup:
+ response = requests.get(source)
+ if response.status_code == 200:
+ title, minimized_body, link_urls, image_urls = cleanup_html(response.text, source)
+ parsed_content = f"Title: {title}, Body: {minimized_body}, Links: {link_urls}, Images: {image_urls}"
+ compressed_document = [Document(page_content=parsed_content)]
+ else:
+ print(f"Failed to retrieve contents from the webpage at url: {source}")
else:
- if self.node_config is not None and self.node_config.get("endpoint") is not None:
-
- loader = AsyncChromiumLoader(
- [source],
- proxies={"http": self.node_config["endpoint"]},
- headless=self.headless,
- )
- else:
- loader = AsyncChromiumLoader(
- [source],
- headless=self.headless,
- )
+ loader_kwargs = {}
+
+ if self.node_config is not None:
+ loader_kwargs = self.node_config.get("loader_kwargs", {})
+ loader = ChromiumLoader([source], headless=self.headless, **loader_kwargs)
document = loader.load()
+
+ title, minimized_body, link_urls, image_urls = cleanup_html(str(document[0].page_content), source)
+ parsed_content = f"Title: {title}, Body: {minimized_body}, Links: {link_urls}, Images: {image_urls}"
+
compressed_document = [
- Document(page_content=remover(str(document[0].page_content)))]
+ Document(page_content=parsed_content, metadata={"source": source})
+ ]
+
+ state.update({self.output[0]: compressed_document, self.output[1]: link_urls, self.output[2]: image_urls})
+
- state.update({self.output[0]: compressed_document})
return state