Skip to content

Python scraper based on AI

add convert function

Marco Vinciguerra8bb560a4893e
diff --git a/pyproject.toml b/pyproject.toml
index 02114c26..e3a820c4 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -33,6 +33,7 @@ dependencies = [
"google==3.0.0",
"undetected-playwright==0.3.0",
"semchunk==1.0.1",
+ "html2text==2024.2.26"
]
license = "MIT"
diff --git a/requirements-dev.lock b/requirements-dev.lock
index 52c5faa4..62de2e2e 100644
--- a/requirements-dev.lock
+++ b/requirements-dev.lock
@@ -21,9 +21,9 @@ altair==5.3.0
# via streamlit
annotated-types==0.7.0
# via pydantic
-anthropic==0.26.1
+anthropic==0.28.1
# via langchain-anthropic
-anyio==4.3.0
+anyio==4.4.0
# via anthropic
# via groq
# via httpx
@@ -42,9 +42,9 @@ beautifulsoup4==4.12.3
# via scrapegraphai
blinker==1.8.2
# via streamlit
-boto3==1.34.113
+boto3==1.34.129
# via langchain-aws
-botocore==1.34.113
+botocore==1.34.129
# via boto3
# via s3transfer
burr==0.22.1
@@ -52,7 +52,7 @@ burr==0.22.1
cachetools==5.3.3
# via google-auth
# via streamlit
-certifi==2024.2.2
+certifi==2024.6.2
# via httpcore
# via httpx
# via requests
@@ -67,7 +67,7 @@ contourpy==1.2.1
# via matplotlib
cycler==0.12.1
# via matplotlib
-dataclasses-json==0.6.6
+dataclasses-json==0.6.7
# via langchain
# via langchain-community
defusedxml==0.7.1
@@ -80,27 +80,26 @@ dnspython==2.6.1
# via email-validator
docutils==0.19
# via sphinx
-email-validator==2.1.1
+email-validator==2.1.2
# via fastapi
faiss-cpu==1.8.0
# via scrapegraphai
fastapi==0.111.0
# via burr
- # via fastapi-pagination
fastapi-cli==0.0.4
# via fastapi
-fastapi-pagination==0.12.24
+fastapi-pagination==0.12.25
# via burr
-filelock==3.14.0
+filelock==3.15.3
# via huggingface-hub
-fonttools==4.52.1
+fonttools==4.53.0
# via matplotlib
free-proxy==1.1.1
# via scrapegraphai
frozenlist==1.4.1
# via aiohttp
# via aiosignal
-fsspec==2024.5.0
+fsspec==2024.6.0
# via huggingface-hub
furo==2024.5.6
# via scrapegraphai
@@ -116,9 +115,9 @@ google-api-core==2.19.0
# via google-ai-generativelanguage
# via google-api-python-client
# via google-generativeai
-google-api-python-client==2.130.0
+google-api-python-client==2.134.0
# via google-generativeai
-google-auth==2.29.0
+google-auth==2.30.0
# via google-ai-generativelanguage
# via google-api-core
# via google-api-python-client
@@ -128,7 +127,7 @@ google-auth-httplib2==0.2.0
# via google-api-python-client
google-generativeai==0.5.4
# via langchain-google-genai
-googleapis-common-protos==1.63.0
+googleapis-common-protos==1.63.1
# via google-api-core
# via grpcio-status
graphviz==0.20.3
@@ -136,9 +135,9 @@ graphviz==0.20.3
# via scrapegraphai
greenlet==3.0.3
# via playwright
-groq==0.8.0
+groq==0.9.0
# via langchain-groq
-grpcio==1.64.0
+grpcio==1.64.1
# via google-api-core
# via grpcio-status
grpcio-status==1.62.2
@@ -160,7 +159,7 @@ httpx==0.27.0
# via fastapi
# via groq
# via openai
-huggingface-hub==0.23.1
+huggingface-hub==0.23.4
# via tokenizers
idna==3.7
# via anyio
@@ -178,7 +177,7 @@ jinja2==3.1.4
# via fastapi
# via pydeck
# via sphinx
-jiter==0.4.0
+jiter==0.4.2
# via anthropic
jmespath==1.0.1
# via boto3
@@ -186,7 +185,7 @@ jmespath==1.0.1
jsonpatch==1.33
# via langchain
# via langchain-core
-jsonpointer==2.4
+jsonpointer==3.0.0
# via jsonpatch
jsonschema==4.22.0
# via altair
@@ -219,7 +218,7 @@ langchain-openai==0.1.6
# via scrapegraphai
langchain-text-splitters==0.0.2
# via langchain
-langsmith==0.1.63
+langsmith==0.1.80
# via langchain
# via langchain-community
# via langchain-core
@@ -231,7 +230,7 @@ markdown-it-py==3.0.0
# via rich
markupsafe==2.1.5
# via jinja2
-marshmallow==3.21.2
+marshmallow==3.21.3
# via dataclasses-json
matplotlib==3.9.0
# via burr
@@ -257,10 +256,10 @@ numpy==1.26.4
# via pydeck
# via sf-hamilton
# via streamlit
-openai==1.30.3
+openai==1.35.0
# via burr
# via langchain-openai
-orjson==3.10.3
+orjson==3.10.5
# via fastapi
# via langsmith
packaging==23.2
@@ -285,7 +284,7 @@ playwright==1.43.0
# via undetected-playwright
pluggy==1.5.0
# via pytest
-proto-plus==1.23.0
+proto-plus==1.24.0
# via google-ai-generativelanguage
# via google-api-core
protobuf==4.25.3
@@ -303,7 +302,7 @@ pyasn1==0.6.0
# via rsa
pyasn1-modules==0.4.0
# via google-auth
-pydantic==2.7.1
+pydantic==2.7.4
# via anthropic
# via burr
# via fastapi
@@ -314,7 +313,7 @@ pydantic==2.7.1
# via langchain-core
# via langsmith
# via openai
-pydantic-core==2.18.2
+pydantic-core==2.18.4
# via pydantic
pydeck==0.9.1
# via streamlit
@@ -352,7 +351,7 @@ referencing==0.35.1
# via jsonschema-specifications
regex==2024.5.15
# via tiktoken
-requests==2.32.2
+requests==2.32.3
# via burr
# via free-proxy
# via google-api-core
@@ -375,7 +374,7 @@ s3transfer==0.10.1
# via boto3
semchunk==1.0.1
# via scrapegraphai
-sf-hamilton==1.63.0
+sf-hamilton==1.66.1
# via burr
shellingham==1.5.4
# via typer
@@ -411,14 +410,14 @@ sphinxcontrib-qthelp==1.0.7
# via sphinx
sphinxcontrib-serializinghtml==1.1.10
# via sphinx
-sqlalchemy==2.0.30
+sqlalchemy==2.0.31
# via langchain
# via langchain-community
starlette==0.37.2
# via fastapi
streamlit==1.35.0
# via burr
-tenacity==8.3.0
+tenacity==8.4.1
# via langchain
# via langchain-community
# via langchain-core
@@ -432,7 +431,7 @@ toml==0.10.2
# via streamlit
toolz==0.12.1
# via altair
-tornado==6.4
+tornado==6.4.1
# via streamlit
tqdm==4.66.4
# via google-generativeai
@@ -442,7 +441,7 @@ tqdm==4.66.4
# via semchunk
typer==0.12.3
# via fastapi-cli
-typing-extensions==4.12.0
+typing-extensions==4.12.2
# via anthropic
# via fastapi
# via fastapi-pagination
@@ -469,15 +468,15 @@ undetected-playwright==0.3.0
# via scrapegraphai
uritemplate==4.1.1
# via google-api-python-client
-urllib3==1.26.18
+urllib3==2.2.2
# via botocore
# via requests
-uvicorn==0.29.0
+uvicorn==0.30.1
# via burr
# via fastapi
uvloop==0.19.0
# via uvicorn
-watchfiles==0.21.0
+watchfiles==0.22.0
# via uvicorn
websockets==12.0
# via uvicorn
diff --git a/requirements.lock b/requirements.lock
index 1dc6ef4f..3bcf5327 100644
--- a/requirements.lock
+++ b/requirements.lock
@@ -15,9 +15,9 @@ aiosignal==1.3.1
# via aiohttp
annotated-types==0.7.0
# via pydantic
-anthropic==0.26.1
+anthropic==0.28.1
# via langchain-anthropic
-anyio==4.3.0
+anyio==4.4.0
# via anthropic
# via groq
# via httpx
@@ -27,20 +27,20 @@ attrs==23.2.0
beautifulsoup4==4.12.3
# via google
# via scrapegraphai
-boto3==1.34.113
+boto3==1.34.129
# via langchain-aws
-botocore==1.34.113
+botocore==1.34.129
# via boto3
# via s3transfer
cachetools==5.3.3
# via google-auth
-certifi==2024.2.2
+certifi==2024.6.2
# via httpcore
# via httpx
# via requests
charset-normalizer==3.3.2
# via requests
-dataclasses-json==0.6.6
+dataclasses-json==0.6.7
# via langchain
# via langchain-community
defusedxml==0.7.1
@@ -51,14 +51,14 @@ distro==1.9.0
# via openai
faiss-cpu==1.8.0
# via scrapegraphai
-filelock==3.14.0
+filelock==3.15.3
# via huggingface-hub
free-proxy==1.1.1
# via scrapegraphai
frozenlist==1.4.1
# via aiohttp
# via aiosignal
-fsspec==2024.5.0
+fsspec==2024.6.0
# via huggingface-hub
google==3.0.0
# via scrapegraphai
@@ -68,9 +68,9 @@ google-api-core==2.19.0
# via google-ai-generativelanguage
# via google-api-python-client
# via google-generativeai
-google-api-python-client==2.130.0
+google-api-python-client==2.134.0
# via google-generativeai
-google-auth==2.29.0
+google-auth==2.30.0
# via google-ai-generativelanguage
# via google-api-core
# via google-api-python-client
@@ -80,16 +80,16 @@ google-auth-httplib2==0.2.0
# via google-api-python-client
google-generativeai==0.5.4
# via langchain-google-genai
-googleapis-common-protos==1.63.0
+googleapis-common-protos==1.63.1
# via google-api-core
# via grpcio-status
graphviz==0.20.3
# via scrapegraphai
greenlet==3.0.3
# via playwright
-groq==0.8.0
+groq==0.9.0
# via langchain-groq
-grpcio==1.64.0
+grpcio==1.64.1
# via google-api-core
# via grpcio-status
grpcio-status==1.62.2
@@ -107,14 +107,14 @@ httpx==0.27.0
# via anthropic
# via groq
# via openai
-huggingface-hub==0.23.1
+huggingface-hub==0.23.4
# via tokenizers
idna==3.7
# via anyio
# via httpx
# via requests
# via yarl
-jiter==0.4.0
+jiter==0.4.2
# via anthropic
jmespath==1.0.1
# via boto3
@@ -122,7 +122,7 @@ jmespath==1.0.1
jsonpatch==1.33
# via langchain
# via langchain-core
-jsonpointer==2.4
+jsonpointer==3.0.0
# via jsonpatch
langchain==0.1.15
# via scrapegraphai
@@ -149,13 +149,13 @@ langchain-openai==0.1.6
# via scrapegraphai
langchain-text-splitters==0.0.2
# via langchain
-langsmith==0.1.63
+langsmith==0.1.80
# via langchain
# via langchain-community
# via langchain-core
lxml==5.2.2
# via free-proxy
-marshmallow==3.21.2
+marshmallow==3.21.3
# via dataclasses-json
minify-html==0.15.0
# via scrapegraphai
@@ -170,9 +170,9 @@ numpy==1.26.4
# via langchain-aws
# via langchain-community
# via pandas
-openai==1.30.3
+openai==1.35.0
# via langchain-openai
-orjson==3.10.3
+orjson==3.10.5
# via langsmith
packaging==23.2
# via huggingface-hub
@@ -183,7 +183,7 @@ pandas==2.2.2
playwright==1.43.0
# via scrapegraphai
# via undetected-playwright
-proto-plus==1.23.0
+proto-plus==1.24.0
# via google-ai-generativelanguage
# via google-api-core
protobuf==4.25.3
@@ -198,7 +198,7 @@ pyasn1==0.6.0
# via rsa
pyasn1-modules==0.4.0
# via google-auth
-pydantic==2.7.1
+pydantic==2.7.4
# via anthropic
# via google-generativeai
# via groq
@@ -206,7 +206,7 @@ pydantic==2.7.1
# via langchain-core
# via langsmith
# via openai
-pydantic-core==2.18.2
+pydantic-core==2.18.4
# via pydantic
pyee==11.1.0
# via playwright
@@ -226,7 +226,7 @@ pyyaml==6.0.1
# via langchain-core
regex==2024.5.15
# via tiktoken
-requests==2.32.2
+requests==2.32.3
# via free-proxy
# via google-api-core
# via huggingface-hub
@@ -250,10 +250,10 @@ sniffio==1.3.1
# via openai
soupsieve==2.5
# via beautifulsoup4
-sqlalchemy==2.0.30
+sqlalchemy==2.0.31
# via langchain
# via langchain-community
-tenacity==8.3.0
+tenacity==8.4.1
# via langchain
# via langchain-community
# via langchain-core
@@ -268,7 +268,7 @@ tqdm==4.66.4
# via openai
# via scrapegraphai
# via semchunk
-typing-extensions==4.12.0
+typing-extensions==4.12.2
# via anthropic
# via google-generativeai
# via groq
@@ -287,7 +287,7 @@ undetected-playwright==0.3.0
# via scrapegraphai
uritemplate==4.1.1
# via google-api-python-client
-urllib3==1.26.18
+urllib3==2.2.2
# via botocore
# via requests
yarl==1.9.4
diff --git a/requirements.txt b/requirements.txt
index 46ae491a..f8a46d54 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -17,4 +17,5 @@ langchain-groq==0.1.3
playwright==1.43.0
langchain-aws==0.1.2
undetected-playwright==0.3.0
-semchunk==1.0.1
\ No newline at end of file
+semchunk==1.0.1
+html2text==2024.2.26
diff --git a/scrapegraphai/nodes/fetch_node.py b/scrapegraphai/nodes/fetch_node.py
index 681ce6fd..79c83364 100644
--- a/scrapegraphai/nodes/fetch_node.py
+++ b/scrapegraphai/nodes/fetch_node.py
@@ -11,7 +11,7 @@ from langchain_community.document_loaders import PyPDFLoader
from langchain_core.documents import Document
from ..docloaders import ChromiumLoader
-from ..utils.cleanup_html import cleanup_html
+from ..utils.convert_to_md import convert_to_md
from ..utils.logging import get_logger
from .base_node import BaseNode
@@ -136,8 +136,7 @@ class FetchNode(BaseNode):
self.logger.info(f"--- (Fetching HTML from: {source}) ---")
if not source.strip():
raise ValueError("No HTML body content found in the local source.")
- 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}"
+ parsed_content = convert_to_md(source)
compressed_document = [
Document(page_content=parsed_content, metadata={"source": "local_dir"})
]
@@ -148,10 +147,7 @@ class FetchNode(BaseNode):
if response.status_code == 200:
if not response.text.strip():
raise ValueError("No HTML body content found in the response.")
- 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}"
+ parsed_content = convert_to_md(source)
compressed_document = [Document(page_content=parsed_content)]
else:
self.logger.warning(
@@ -171,10 +167,7 @@ class FetchNode(BaseNode):
if not document or not document[0].page_content.strip():
raise ValueError("No HTML body content found in the document fetched by ChromiumLoader.")
- 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}"
+ parsed_content = convert_to_md(source)
compressed_document = [
Document(page_content=parsed_content, metadata={"source": source})
@@ -183,8 +176,6 @@ class FetchNode(BaseNode):
state.update(
{
self.output[0]: compressed_document,
- self.output[1]: link_urls,
- self.output[2]: image_urls,
}
)
diff --git a/scrapegraphai/utils/__init__.py b/scrapegraphai/utils/__init__.py
index d2218489..707d2b18 100644
--- a/scrapegraphai/utils/__init__.py
+++ b/scrapegraphai/utils/__init__.py
@@ -10,3 +10,4 @@ from .save_audio_from_bytes import save_audio_from_bytes
from .sys_dynamic_import import dynamic_import, srcfile_import
from .cleanup_html import cleanup_html
from .logging import *
+from .convert_to_md import convert_to_md
diff --git a/scrapegraphai/utils/convert_to_md.py b/scrapegraphai/utils/convert_to_md.py
new file mode 100644
index 00000000..4350befa
--- /dev/null
+++ b/scrapegraphai/utils/convert_to_md.py
@@ -0,0 +1,21 @@
+"""
+convert_to_md modul
+"""
+import html2text
+
+def convert_to_md(html):
+ """ Convert HTML to Markdown.
+ This function uses the html2text library to convert the provided HTML content to Markdown
+ format.
+ The function returns the converted Markdown content as a string.
+
+ Args: html (str): The HTML content to be converted.
+
+ Returns: str: The equivalent Markdown content.
+
+ Example: >>> convert_to_md("<html><body><p>This is a paragraph.</p><h1>This is a heading.</h1></body></html>")
+ 'This is a paragraph.\n\n# This is a heading.'
+
+ Note: All the styles and links are ignored during the conversion. """
+ converter = html2text.HTML2Text()
+ return converter.handle(html)