Skip to content

Python scraper based on AI

gode generator v0.1

Matteo Vedovati5b579b323fbd
diff --git a/examples/code_generation/simple_with_schema.py b/examples/code_generation/simple_with_schema.py
new file mode 100644
index 00000000..c4803c62
--- /dev/null
+++ b/examples/code_generation/simple_with_schema.py
@@ -0,0 +1,53 @@
+"""
+Basic example of scraping pipeline using Code Generator with schema
+"""
+
+import os, json
+from typing import List
+from dotenv import load_dotenv
+from langchain_core.pydantic_v1 import BaseModel, Field
+from scrapegraphai.graphs import CodeGeneratorGraph
+
+load_dotenv()
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+openai_key = os.getenv("OPENAI_APIKEY")
+
+graph_config = {
+ "llm": {
+ "api_key":openai_key,
+ "model": "openai/gpt-4o",\
+ },
+ "library": "beautifulsoup",
+ "verbose": True,
+ "headless": False,
+ "reduction": 2,
+}
+
+# ************************************************
+# Create the SmartScraperGraph instance and run it
+# ************************************************
+
+code_generator_graph = CodeGeneratorGraph(
+ prompt="List me all the projects with their description",
+ source="https://perinim.github.io/projects/",
+ schema=Projects,
+ config=graph_config
+)
+
+result = code_generator_graph.run()
+print(result)
diff --git a/requirements-dev.lock b/requirements-dev.lock
index fd04d800..c271122c 100644
--- a/requirements-dev.lock
+++ b/requirements-dev.lock
@@ -6,6 +6,8 @@
# features: []
# all-features: false
# with-sources: false
+# generate-hashes: false
+# universal: false
-e file:.
aiofiles==24.1.0
@@ -71,6 +73,7 @@ cycler==0.12.1
dataclasses-json==0.6.7
# via langchain-community
dill==0.3.8
+ # via multiprocess
# via pylint
distro==1.9.0
# via openai
@@ -87,6 +90,7 @@ fastapi-pagination==0.12.26
# via burr
filelock==3.15.4
# via huggingface-hub
+ # via transformers
fonttools==4.53.1
# via matplotlib
free-proxy==1.1.1
@@ -129,6 +133,7 @@ graphviz==0.20.3
# via burr
greenlet==3.0.3
# via playwright
+ # via sqlalchemy
grpcio==1.65.4
# via google-api-core
# via grpcio-status
@@ -152,6 +157,7 @@ httpx-sse==0.4.0
# via langchain-mistralai
huggingface-hub==0.24.5
# via tokenizers
+ # via transformers
idna==3.7
# via anyio
# via httpx
@@ -235,9 +241,13 @@ mdurl==0.1.2
# via markdown-it-py
minify-html==0.15.0
# via scrapegraphai
+mpire==2.10.2
+ # via semchunk
multidict==6.0.5
# via aiohttp
# via yarl
+multiprocess==0.70.16
+ # via mpire
mypy-extensions==1.0.0
# via typing-inspect
narwhals==1.3.0
@@ -254,6 +264,7 @@ numpy==1.26.4
# via pydeck
# via sf-hamilton
# via streamlit
+ # via transformers
ollama==0.3.2
# via langchain-ollama
openai==1.40.3
@@ -271,6 +282,7 @@ packaging==24.1
# via pytest
# via sphinx
# via streamlit
+ # via transformers
pandas==2.2.2
# via scrapegraphai
# via sf-hamilton
@@ -320,6 +332,7 @@ pyee==11.1.0
# via playwright
pygments==2.18.0
# via furo
+ # via mpire
# via rich
# via sphinx
pylint==3.2.6
@@ -342,11 +355,13 @@ pyyaml==6.0.2
# via langchain
# via langchain-community
# via langchain-core
+ # via transformers
referencing==0.35.1
# via jsonschema
# via jsonschema-specifications
regex==2024.7.24
# via tiktoken
+ # via transformers
requests==2.32.3
# via burr
# via free-proxy
@@ -358,6 +373,7 @@ requests==2.32.3
# via sphinx
# via streamlit
# via tiktoken
+ # via transformers
rich==13.7.1
# via streamlit
rpds-py==0.20.0
@@ -367,6 +383,10 @@ rsa==4.9
# via google-auth
s3transfer==0.10.2
# via boto3
+safetensors==0.4.5
+ # via transformers
+semchunk==2.2.0
+ # via scrapegraphai
sf-hamilton==1.73.1
# via burr
six==1.16.0
@@ -416,6 +436,7 @@ tiktoken==0.7.0
# via scrapegraphai
tokenizers==0.19.1
# via langchain-mistralai
+ # via transformers
toml==0.10.2
# via streamlit
tomli==2.0.1
@@ -428,8 +449,13 @@ tornado==6.4.1
tqdm==4.66.5
# via google-generativeai
# via huggingface-hub
+ # via mpire
# via openai
# via scrapegraphai
+ # via semchunk
+ # via transformers
+transformers==4.44.2
+ # via scrapegraphai
typing-extensions==4.12.2
# via altair
# via anyio
@@ -464,6 +490,8 @@ urllib3==1.26.19
# via requests
uvicorn==0.30.5
# via burr
+watchdog==4.0.2
+ # via streamlit
yarl==1.9.4
# via aiohttp
zipp==3.20.1
diff --git a/requirements.lock b/requirements.lock
index b34c9290..f05c6db4 100644
--- a/requirements.lock
+++ b/requirements.lock
@@ -6,6 +6,8 @@
# features: []
# all-features: false
# with-sources: false
+# generate-hashes: false
+# universal: false
-e file:.
aiohttp==3.9.5
@@ -41,6 +43,8 @@ charset-normalizer==3.3.2
# via requests
dataclasses-json==0.6.7
# via langchain-community
+dill==0.3.8
+ # via multiprocess
distro==1.9.0
# via openai
exceptiongroup==1.2.2
@@ -49,6 +53,7 @@ faiss-cpu==1.8.0.post1
# via scrapegraphai
filelock==3.15.4
# via huggingface-hub
+ # via transformers
free-proxy==1.1.1
# via scrapegraphai
frozenlist==1.4.1
@@ -81,6 +86,7 @@ googleapis-common-protos==1.63.2
# via grpcio-status
greenlet==3.0.3
# via playwright
+ # via sqlalchemy
grpcio==1.65.1
# via google-api-core
# via grpcio-status
@@ -103,6 +109,7 @@ httpx-sse==0.4.0
# via langchain-mistralai
huggingface-hub==0.24.1
# via tokenizers
+ # via transformers
idna==3.7
# via anyio
# via httpx
@@ -153,9 +160,13 @@ marshmallow==3.21.3
# via dataclasses-json
minify-html==0.15.0
# via scrapegraphai
+mpire==2.10.2
+ # via semchunk
multidict==6.0.5
# via aiohttp
# via yarl
+multiprocess==0.70.16
+ # via mpire
mypy-extensions==1.0.0
# via typing-inspect
numpy==1.26.4
@@ -164,6 +175,7 @@ numpy==1.26.4
# via langchain-aws
# via langchain-community
# via pandas
+ # via transformers
ollama==0.3.2
# via langchain-ollama
openai==1.41.0
@@ -175,6 +187,7 @@ packaging==24.1
# via huggingface-hub
# via langchain-core
# via marshmallow
+ # via transformers
pandas==2.2.2
# via scrapegraphai
playwright==1.45.1
@@ -205,6 +218,8 @@ pydantic-core==2.20.1
# via pydantic
pyee==11.1.0
# via playwright
+pygments==2.18.0
+ # via mpire
pyparsing==3.1.2
# via httplib2
python-dateutil==2.9.0.post0
@@ -219,8 +234,10 @@ pyyaml==6.0.1
# via langchain
# via langchain-community
# via langchain-core
+ # via transformers
regex==2024.5.15
# via tiktoken
+ # via transformers
requests==2.32.3
# via free-proxy
# via google-api-core
@@ -229,10 +246,15 @@ requests==2.32.3
# via langchain-community
# via langsmith
# via tiktoken
+ # via transformers
rsa==4.9
# via google-auth
s3transfer==0.10.2
# via boto3
+safetensors==0.4.5
+ # via transformers
+semchunk==2.2.0
+ # via scrapegraphai
six==1.16.0
# via python-dateutil
sniffio==1.3.1
@@ -253,11 +275,17 @@ tiktoken==0.7.0
# via scrapegraphai
tokenizers==0.19.1
# via langchain-mistralai
+ # via transformers
tqdm==4.66.4
# via google-generativeai
# via huggingface-hub
+ # via mpire
# via openai
# via scrapegraphai
+ # via semchunk
+ # via transformers
+transformers==4.44.2
+ # via scrapegraphai
typing-extensions==4.12.2
# via anyio
# via google-generativeai
diff --git a/scrapegraphai/code_gen/code_exec_test.py b/scrapegraphai/code_gen/code_exec_test.py
new file mode 100644
index 00000000..95d2eefd
--- /dev/null
+++ b/scrapegraphai/code_gen/code_exec_test.py
@@ -0,0 +1,526 @@
+import ast
+import sys
+from io import StringIO
+from bs4 import BeautifulSoup
+import re
+
+generated_code = "def extract_data(html: str) -> dict:\n from bs4 import BeautifulSoup\n import re\n\n # Parse the HTML content using BeautifulSoup\n soup = BeautifulSoup(html, 'html.parser')\n\n # Initialize the projects list\n projects = []\n\n # Find all <a> tags that contain project entries\n project_links = soup.find_all('a', href=True)\n\n # Iterate through each project link to extract title and description\n for link in project_links:\n # Check if the link contains an image and text\n img_tag = link.find('img')\n if img_tag and link.string:\n # Extract the full text and split it into title and description\n full_text = link.string.strip()\n # Use regex to separate title and description\n match = re.match(r'^(.*?)(?:\\s*-\\s*(.*))?$', full_text)\n if match:\n title = match.group(1).strip()\n description = match.group(2).strip() if match.group(2) else ''\n \n # Append the project data to the projects list\n projects.append({\n 'title': title,\n 'description': description\n })\n\n # Return the structured data as a dictionary\n return {\n 'projects': projects\n }\n"
+html = """
+<html lang="en" class="" data-theme="dark"><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Projects | Marco Perini</title> <meta name="author" content="Marco Perini"> <meta name="description" content="Personal Porfolio Website "> <meta name="keywords" content="jekyll, jekyll-theme, academic-website, portfolio-website, robotics, machine-learning, computer-vision, artificial-intelligence, deep-learning, data-science, data-analysis, data-visualization, reinforcement-learning, computer-science, computer-graphics, computer-architecture, computer-networks, computer-security, computer-aided-design, computer-algebra, computer-alg, hardware"> <link rel="stylesheet" href="/assets/css/bootstrap.min.css?a4b3f509e79c54a512b890d73235ef04"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/mdb.min.css" integrity="sha256-jpjYvU3G3N6nrrBwXJoVEYI/0zw8htfFnhT9ljN3JJw=" crossorigin="anonymous"> <link defer="" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css"> <link rel="stylesheet" href="/assets/css/academicons.min.css?f0b7046b84e425c55f3463ac249818f5"> <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:100,300,400,500,700|Material+Icons"> <link rel="stylesheet" href="/assets/css/jekyll-pygments-themes-github.css?19f3075a2d19613090fe9e16b564e1fe" media="none" id="highlight_theme_light"> <link rel="shortcut icon" href="data:image/svg+xml,<svg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20100%20100%22><text%20y=%22.9em%22%20font-size=%2290%22>%F0%9F%A6%BE</text></svg>"> <link rel="stylesheet" href="/assets/css/main.css?d41d8cd98f00b204e9800998ecf8427e"> <link rel="canonical" href="https://perinim.github.io/projects/"> <link rel="stylesheet" href="/assets/css/jekyll-pygments-themes-native.css?e74e74bf055e5729d44a7d031a5ca6a5" media="" id="highlight_theme_dark"> <script src="/assets/js/theme.js?96d6b3e1c3604aca8b6134c7afdd5db6"></script> <script src="/assets/js/dark_mode.js?9b17307bb950ffa2e34be0227f53558f"></script> <style type="text/css">/* Chart.js */
+@-webkit-keyframes chartjs-render-animation{from{opacity:0.99}to{opacity:1}}@keyframes chartjs-render-animation{from{opacity:0.99}to{opacity:1}}.chartjs-render-monitor{-webkit-animation:chartjs-render-animation 0.001s;animation:chartjs-render-animation 0.001s;}</style><script id="altmetric-embed-js" src="https://d1bxh8uas1mnw7.cloudfront.net/assets/altmetric_badges-2f3c1a827c4dee5fa0ff35ec229b9204ae106583cc99636c724152d1f7acea04.js"></script><style type="text/css">.medium-zoom-overlay{position:fixed;top:0;right:0;bottom:0;left:0;opacity:0;transition:opacity .3s;will-change:opacity}.medium-zoom--opened .medium-zoom-overlay{cursor:pointer;cursor:zoom-out;opacity:1}.medium-zoom-image{cursor:pointer;cursor:zoom-in;transition:transform .3s cubic-bezier(.2,0,.2,1)!important}.medium-zoom-image--hidden{visibility:hidden}.medium-zoom-image--opened{position:relative;cursor:pointer;cursor:zoom-out;will-change:transform}</style><style type="text/css">.CtxtMenu_InfoClose { top:.2em; right:.2em;}
+.CtxtMenu_InfoContent { overflow:auto; text-align:left; font-size:80%; padding:.4em .6em; border:1px inset; margin:1em 0px; max-height:20em; max-width:30em; background-color:#EEEEEE; white-space:normal;}
+.CtxtMenu_Info.CtxtMenu_MousePost {outline:none;}
+.CtxtMenu_Info { position:fixed; left:50%; width:auto; text-align:center; border:3px outset; padding:1em 2em; background-color:#DDDDDD; color:black; cursor:default; font-family:message-box; font-size:120%; font-style:normal; text-indent:0; text-transform:none; line-height:normal; letter-spacing:normal; word-spacing:normal; word-wrap:normal; white-space:nowrap; float:none; z-index:201; border-radius: 15px; /* Opera 10.5 and IE9 */ -webkit-border-radius:15px; /* Safari and Chrome */ -moz-border-radius:15px; /* Firefox */ -khtml-border-radius:15px; /* Konqueror */ box-shadow:0px 10px 20px #808080; /* Opera 10.5 and IE9 */ -webkit-box-shadow:0px 10px 20px #808080; /* Safari 3 & Chrome */ -moz-box-shadow:0px 10px 20px #808080; /* Forefox 3.5 */ -khtml-box-shadow:0px 10px 20px #808080; /* Konqueror */ filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color="gray", Positive="true"); /* IE */}
+</style><style type="text/css">.CtxtMenu_MenuClose { position:absolute; cursor:pointer; display:inline-block; border:2px solid #AAA; border-radius:18px; -webkit-border-radius: 18px; /* Safari and Chrome */ -moz-border-radius: 18px; /* Firefox */ -khtml-border-radius: 18px; /* Konqueror */ font-family: "Courier New", Courier; font-size:24px; color:#F0F0F0}
+.CtxtMenu_MenuClose span { display:block; background-color:#AAA; border:1.5px solid; border-radius:18px; -webkit-border-radius: 18px; /* Safari and Chrome */ -moz-border-radius: 18px; /* Firefox */ -khtml-border-radius: 18px; /* Konqueror */ line-height:0; padding:8px 0 6px /* may need to be browser-specific */}
+.CtxtMenu_MenuClose:hover { color:white!important; border:2px solid #CCC!important}
+.CtxtMenu_MenuClose:hover span { background-color:#CCC!important}
+.CtxtMenu_MenuClose:hover:focus { outline:none}
+</style><style type="text/css">.CtxtMenu_Menu { position:absolute; background-color:white; color:black; width:auto; padding:5px 0px; border:1px solid #CCCCCC; margin:0; cursor:default; font: menu; text-align:left; text-indent:0; text-transform:none; line-height:normal; letter-spacing:normal; word-spacing:normal; word-wrap:normal; white-space:nowrap; float:none; z-index:201; border-radius: 5px; /* Opera 10.5 and IE9 */ -webkit-border-radius: 5px; /* Safari and Chrome */ -moz-border-radius: 5px; /* Firefox */ -khtml-border-radius: 5px; /* Konqueror */ box-shadow:0px 10px 20px #808080; /* Opera 10.5 and IE9 */ -webkit-box-shadow:0px 10px 20px #808080; /* Safari 3 & Chrome */ -moz-box-shadow:0px 10px 20px #808080; /* Forefox 3.5 */ -khtml-box-shadow:0px 10px 20px #808080; /* Konqueror */}
+.CtxtMenu_MenuItem { padding: 1px 2em; background:transparent;}
+.CtxtMenu_MenuArrow { position:absolute; right:.5em; padding-top:.25em; color:#666666; font-family: null; font-size: .75em}
+.CtxtMenu_MenuActive .CtxtMenu_MenuArrow {color:white}
+.CtxtMenu_MenuArrow.CtxtMenu_RTL {left:.5em; right:auto}
+.CtxtMenu_MenuCheck { position:absolute; left:.7em; font-family: null}
+.CtxtMenu_MenuCheck.CtxtMenu_RTL { right:.7em; left:auto }
+.CtxtMenu_MenuRadioCheck { position:absolute; left: .7em;}
+.CtxtMenu_MenuRadioCheck.CtxtMenu_RTL { right: .7em; left:auto}
+.CtxtMenu_MenuInputBox { padding-left: 1em; right:.5em; color:#666666; font-family: null;}
+.CtxtMenu_MenuInputBox.CtxtMenu_RTL { left: .1em;}
+.CtxtMenu_MenuComboBox { left:.1em; padding-bottom:.5em;}
+.CtxtMenu_MenuSlider { left: .1em;}
+.CtxtMenu_SliderValue { position:absolute; right:.1em; padding-top:.25em; color:#333333; font-size: .75em}
+.CtxtMenu_SliderBar { outline: none; background: #d3d3d3}
+.CtxtMenu_MenuLabel { padding: 1px 2em 3px 1.33em; font-style:italic}
+.CtxtMenu_MenuRule { border-top: 1px solid #DDDDDD; margin: 4px 3px;}
+.CtxtMenu_MenuDisabled { color:GrayText}
+.CtxtMenu_MenuActive { background-color: #606872; color: white;}
+.CtxtMenu_MenuDisabled:focus { background-color: #E8E8E8}
+.CtxtMenu_MenuLabel:focus { background-color: #E8E8E8}
+.CtxtMenu_ContextMenu:focus { outline:none}
+.CtxtMenu_ContextMenu .CtxtMenu_MenuItem:focus { outline:none}
+.CtxtMenu_SelectionMenu { position:relative; float:left; border-bottom: none; -webkit-box-shadow:none; -webkit-border-radius:0px; }
+.CtxtMenu_SelectionItem { padding-right: 1em;}
+.CtxtMenu_Selection { right: 40%; width:50%; }
+.CtxtMenu_SelectionBox { padding: 0em; max-height:20em; max-width: none; background-color:#FFFFFF;}
+.CtxtMenu_SelectionDivider { clear: both; border-top: 2px solid #000000;}
+.CtxtMenu_Menu .CtxtMenu_MenuClose { top:-10px; left:-10px}
+</style><style id="MJX-CHTML-styles">
+mjx-container[jax="CHTML"] {
+ line-height: 0;
+}
+
+mjx-container [space="1"] {
+ margin-left: .111em;
+}
+
+mjx-container [space="2"] {
+ margin-left: .167em;
+}
+
+mjx-container [space="3"] {
+ margin-left: .222em;
+}
+
+mjx-container [space="4"] {
+ margin-left: .278em;
+}
+
+mjx-container [space="5"] {
+ margin-left: .333em;
+}
+
+mjx-container [rspace="1"] {
+ margin-right: .111em;
+}
+
+mjx-container [rspace="2"] {
+ margin-right: .167em;
+}
+
+mjx-container [rspace="3"] {
+ margin-right: .222em;
+}
+
+mjx-container [rspace="4"] {
+ margin-right: .278em;
+}
+
+mjx-container [rspace="5"] {
+ margin-right: .333em;
+}
+
+mjx-container [size="s"] {
+ font-size: 70.7%;
+}
+
+mjx-container [size="ss"] {
+ font-size: 50%;
+}
+
+mjx-container [size="Tn"] {
+ font-size: 60%;
+}
+
+mjx-container [size="sm"] {
+ font-size: 85%;
+}
+
+mjx-container [size="lg"] {
+ font-size: 120%;
+}
+
+mjx-container [size="Lg"] {
+ font-size: 144%;
+}
+
+mjx-container [size="LG"] {
+ font-size: 173%;
+}
+
+mjx-container [size="hg"] {
+ font-size: 207%;
+}
+
+mjx-container [size="HG"] {
+ font-size: 249%;
+}
+
+mjx-container [width="full"] {
+ width: 100%;
+}
+
+mjx-box {
+ display: inline-block;
+}
+
+mjx-block {
+ display: block;
+}
+
+mjx-itable {
+ display: inline-table;
+}
+
+mjx-row {
+ display: table-row;
+}
+
+mjx-row > * {
+ display: table-cell;
+}
+
+mjx-mtext {
+ display: inline-block;
+}
+
+mjx-mstyle {
+ display: inline-block;
+}
+
+mjx-merror {
+ display: inline-block;
+ color: red;
+ background-color: yellow;
+}
+
+mjx-mphantom {
+ visibility: hidden;
+}
+
+_::-webkit-full-page-media, _:future, :root mjx-container {
+ will-change: opacity;
+}
+
+mjx-assistive-mml {
+ position: absolute !important;
+ top: 0px;
+ left: 0px;
+ clip: rect(1px, 1px, 1px, 1px);
+ padding: 1px 0px 0px 0px !important;
+ border: 0px !important;
+ display: block !important;
+ width: auto !important;
+ overflow: hidden !important;
+ -webkit-touch-callout: none;
+ -webkit-user-select: none;
+ -khtml-user-select: none;
+ -moz-user-select: none;
+ -ms-user-select: none;
+ user-select: none;
+}
+
+mjx-assistive-mml[display="block"] {
+ width: 100% !important;
+}
+
+mjx-c::before {
+ display: block;
+ width: 0;
+}
+
+.MJX-TEX {
+ font-family: MJXZERO, MJXTEX;
+}
+
+.TEX-B {
+ font-family: MJXZERO, MJXTEX-B;
+}
+
+.TEX-I {
+ font-family: MJXZERO, MJXTEX-I;
+}
+
+.TEX-MI {
+ font-family: MJXZERO, MJXTEX-MI;
+}
+
+.TEX-BI {
+ font-family: MJXZERO, MJXTEX-BI;
+}
+
+.TEX-S1 {
+ font-family: MJXZERO, MJXTEX-S1;
+}
+
+.TEX-S2 {
+ font-family: MJXZERO, MJXTEX-S2;
+}
+
+.TEX-S3 {
+ font-family: MJXZERO, MJXTEX-S3;
+}
+
+.TEX-S4 {
+ font-family: MJXZERO, MJXTEX-S4;
+}
+
+.TEX-A {
+ font-family: MJXZERO, MJXTEX-A;
+}
+
+.TEX-C {
+ font-family: MJXZERO, MJXTEX-C;
+}
+
+.TEX-CB {
+ font-family: MJXZERO, MJXTEX-CB;
+}
+
+.TEX-FR {
+ font-family: MJXZERO, MJXTEX-FR;
+}
+
+.TEX-FRB {
+ font-family: MJXZERO, MJXTEX-FRB;
+}
+
+.TEX-SS {
+ font-family: MJXZERO, MJXTEX-SS;
+}
+
+.TEX-SSB {
+ font-family: MJXZERO, MJXTEX-SSB;
+}
+
+.TEX-SSI {
+ font-family: MJXZERO, MJXTEX-SSI;
+}
+
+.TEX-SC {
+ font-family: MJXZERO, MJXTEX-SC;
+}
+
+.TEX-T {
+ font-family: MJXZERO, MJXTEX-T;
+}
+
+.TEX-V {
+ font-family: MJXZERO, MJXTEX-V;
+}
+
+.TEX-VB {
+ font-family: MJXZERO, MJXTEX-VB;
+}
+
+mjx-stretchy-v mjx-c, mjx-stretchy-h mjx-c {
+ font-family: MJXZERO, MJXTEX-S1, MJXTEX-S4, MJXTEX, MJXTEX-A ! important;
+}
+
+@font-face /* 0 */ {
+ font-family: MJXZERO;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Zero.woff") format("woff");
+}
+
+@font-face /* 1 */ {
+ font-family: MJXTEX;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Main-Regular.woff") format("woff");
+}
+
+@font-face /* 2 */ {
+ font-family: MJXTEX-B;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Main-Bold.woff") format("woff");
+}
+
+@font-face /* 3 */ {
+ font-family: MJXTEX-I;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Math-Italic.woff") format("woff");
+}
+
+@font-face /* 4 */ {
+ font-family: MJXTEX-MI;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Main-Italic.woff") format("woff");
+}
+
+@font-face /* 5 */ {
+ font-family: MJXTEX-BI;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Math-BoldItalic.woff") format("woff");
+}
+
+@font-face /* 6 */ {
+ font-family: MJXTEX-S1;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Size1-Regular.woff") format("woff");
+}
+
+@font-face /* 7 */ {
+ font-family: MJXTEX-S2;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Size2-Regular.woff") format("woff");
+}
+
+@font-face /* 8 */ {
+ font-family: MJXTEX-S3;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Size3-Regular.woff") format("woff");
+}
+
+@font-face /* 9 */ {
+ font-family: MJXTEX-S4;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Size4-Regular.woff") format("woff");
+}
+
+@font-face /* 10 */ {
+ font-family: MJXTEX-A;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_AMS-Regular.woff") format("woff");
+}
+
+@font-face /* 11 */ {
+ font-family: MJXTEX-C;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Regular.woff") format("woff");
+}
+
+@font-face /* 12 */ {
+ font-family: MJXTEX-CB;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Calligraphic-Bold.woff") format("woff");
+}
+
+@font-face /* 13 */ {
+ font-family: MJXTEX-FR;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Fraktur-Regular.woff") format("woff");
+}
+
+@font-face /* 14 */ {
+ font-family: MJXTEX-FRB;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Fraktur-Bold.woff") format("woff");
+}
+
+@font-face /* 15 */ {
+ font-family: MJXTEX-SS;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Regular.woff") format("woff");
+}
+
+@font-face /* 16 */ {
+ font-family: MJXTEX-SSB;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Bold.woff") format("woff");
+}
+
+@font-face /* 17 */ {
+ font-family: MJXTEX-SSI;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_SansSerif-Italic.woff") format("woff");
+}
+
+@font-face /* 18 */ {
+ font-family: MJXTEX-SC;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Script-Regular.woff") format("woff");
+}
+
+@font-face /* 19 */ {
+ font-family: MJXTEX-T;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Typewriter-Regular.woff") format("woff");
+}
+
+@font-face /* 20 */ {
+ font-family: MJXTEX-V;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Vector-Regular.woff") format("woff");
+}
+
+@font-face /* 21 */ {
+ font-family: MJXTEX-VB;
+ src: url("https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2/MathJax_Vector-Bold.woff") format("woff");
+}
+</style><link rel="stylesheet" href="https://badge.dimensions.ai/badge.css"></head> <body class="fixed-top-nav " data-new-gr-c-s-check-loaded="14.1196.0" data-gr-ext-installed="" style="padding-top: 57px;"> <header> <nav id="navbar" class="navbar navbar-light navbar-expand-sm fixed-top"> <div class="container"> <a class="navbar-brand title font-weight-lighter" href="/"><span class="font-weight-bold">Marco&nbsp;</span>Perini</a> <button class="navbar-toggler collapsed ml-auto" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar top-bar"></span> <span class="icon-bar middle-bar"></span> <span class="icon-bar bottom-bar"></span> </button> <div class="collapse navbar-collapse text-right" id="navbarNav"> <ul class="navbar-nav ml-auto flex-nowrap"> <li class="nav-item "> <a class="nav-link" href="/">About</a> </li> <li class="nav-item dropdown active"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Projects<span class="sr-only">(current)</span></a> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="/projects/">Projects</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/competitions/">Competitions</a> </div> </li> <li class="nav-item "> <a class="nav-link" href="/cv/">CV</a> </li> <li class="toggle-container"> <button id="light-toggle" title="Change theme"> <i class="fa-solid fa-moon"></i> <i class="fa-solid fa-sun"></i> </button> </li> </ul> </div> </div> </nav> <progress id="progress" value="40" max="140" style="top: 57px;"> <div class="progress-container"> <span class="progress-bar"></span> </div> </progress> </header> <div class="container mt-5"> <div class="post"> <header class="post-header"> <h1 class="post-title">Projects</h1> <p class="post-description"></p> </header> <article> <div class="projects"> <div class="grid" style="position: relative; height: 803.703px;"> <div class="grid-sizer"></div> <div class="grid-item" style="position: absolute; left: 0px; top: 0px;"> <a href="/projects/rotary-pendulum-rl/"> <div class="card hoverable"> <figure> <picture> <source class="responsive-img-srcset" media="(max-width: 480px)" srcset="/assets/img/rotary_pybullet-480.webp"> <source class="responsive-img-srcset" media="(max-width: 800px)" srcset="/assets/img/rotary_pybullet-800.webp"> <source class="responsive-img-srcset" media="(max-width: 1400px)" srcset="/assets/img/rotary_pybullet-1400.webp"> <img src="/assets/img/rotary_pybullet.jpg" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture> </figure> <div class="card-body"> <h4 class="card-title">Rotary Pendulum RL</h4> <p class="card-text">Open Source project aimed at controlling a real life rotary pendulum using RL algorithms</p> <div class="row ml-1 mr-1 p-0"> </div> </div> </div> </a> </div> <div class="grid-sizer"></div> <div class="grid-item" style="position: absolute; left: 260px; top: 0px;"> <a href="https://github.com/PeriniM/DQN-SwingUp" rel="external nofollow noopener" target="_blank"> <div class="card hoverable"> <figure> <picture> <source class="responsive-img-srcset" media="(max-width: 480px)" srcset="/assets/img/value-policy-heatmaps-480.webp"> <source class="responsive-img-srcset" media="(max-width: 800px)" srcset="/assets/img/value-policy-heatmaps-800.webp"> <source class="responsive-img-srcset" media="(max-width: 1400px)" srcset="/assets/img/value-policy-heatmaps-1400.webp"> <img src="/assets/img/value-policy-heatmaps.jpg" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture> </figure> <div class="card-body"> <h4 class="card-title">DQN Implementation from scratch</h4> <p class="card-text">Developed a Deep Q-Network algorithm to train a simple and double pendulum</p> <div class="row ml-1 mr-1 p-0"> </div> </div> </div> </a> </div> <div class="grid-sizer"></div> <div class="grid-item" style="position: absolute; left: 520px; top: 0px;"> <a href="https://github.com/PeriniM/Multi-Agents-HAED" rel="external nofollow noopener" target="_blank"> <div class="card hoverable"> <figure> <picture> <source class="responsive-img-srcset" media="(max-width: 480px)" srcset="/assets/img/multi_agents_haed.gif-480.webp"> <source class="responsive-img-srcset" media="(max-width: 800px)" srcset="/assets/img/multi_agents_haed.gif-800.webp"> <source class="responsive-img-srcset" media="(max-width: 1400px)" srcset="/assets/img/multi_agents_haed.gif-1400.webp"> <img src="/assets/img/multi_agents_haed.gif" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture> </figure> <div class="card-body"> <h4 class="card-title">Multi Agents HAED</h4> <p class="card-text">University project which focuses on simulating a multi-agent system to perform environment mapping. Agents, equipped with sensors, explore and record their surroundings, considering uncertainties in their readings.</p> <div class="row ml-1 mr-1 p-0"> </div> </div> </div> </a> </div> <div class="grid-sizer"></div> <div class="grid-item" style="position: absolute; left: 0px; top: 447.453px;"> <a href="/projects/wireless-esc-drone/"> <div class="card hoverable"> <figure> <picture> <source class="responsive-img-srcset" media="(max-width: 480px)" srcset="/assets/img/wireless_esc.gif-480.webp"> <source class="responsive-img-srcset" media="(max-width: 800px)" srcset="/assets/img/wireless_esc.gif-800.webp"> <source class="responsive-img-srcset" media="(max-width: 1400px)" srcset="/assets/img/wireless_esc.gif-1400.webp"> <img src="/assets/img/wireless_esc.gif" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture> </figure> <div class="card-body"> <h4 class="card-title">Wireless ESC for Modular Drones</h4> <p class="card-text">Modular drone architecture proposal and proof of concept. The project received maximum grade.</p> <div class="row ml-1 mr-1 p-0"> </div> </div> </div> </a> </div> </div> </div> </article> </div> </div> <footer class="fixed-bottom"> <div class="container mt-0"> © Copyright 2023 Marco Perini. Powered by <a href="https://jekyllrb.com/" target="_blank" rel="external nofollow noopener">Jekyll</a> with <a href="https://github.com/alshedivat/al-folio" rel="external nofollow noopener" target="_blank">al-folio</a> theme. Hosted by <a href="https://pages.github.com/" target="_blank" rel="external nofollow noopener">GitHub Pages</a>. </div> </footer> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script src="/assets/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/js/mdb.min.js" integrity="sha256-NdbiivsvWt7VYCt6hYNT3h/th9vSTL4EDWeGs5SN3DA=" crossorigin="anonymous"></script> <script defer="" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/masonry.pkgd.min.js" integrity="sha256-Nn1q/fx0H7SNLZMQ5Hw5JLaTRZp0yILA/FRexe19VdI=" crossorigin="anonymous"></script> <script defer="" src="https://cdn.jsdelivr.net/npm/imagesloaded@4/imagesloaded.pkgd.min.js"></script> <script defer="" src="/assets/js/masonry.js" type="text/javascript"></script> <script defer="" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/medium-zoom.min.js" integrity="sha256-7PhEpEWEW0XXQ0k6kQrPKwuoIomz8R8IYyuU1Qew4P8=" crossorigin="anonymous"></script> <script defer="" src="/assets/js/zoom.js?7b30caa5023af4af8408a472dc4e1ebb"></script> <script defer="" src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script> <script src="/assets/js/no_defer.js?d633890033921b33e0ceb13d22340a9c"></script> <script defer="" src="/assets/js/common.js?acdb9690d7641b2f8d40529018c71a01"></script> <script defer="" src="/assets/js/copy_code.js?07b8786bab9b4abe90d10e61f7d12ff7" type="text/javascript"></script> <script async="" src="https://d1bxh8uas1mnw7.cloudfront.net/assets/embed.js"></script> <script async="" src="https://badge.dimensions.ai/badge.js"></script> <script type="text/javascript">window.MathJax={tex:{tags:"ams"}};</script> <script defer="" type="text/javascript" id="MathJax-script" src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js"></script> <script defer="" src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> <script async="" src="https://www.googletagmanager.com/gtag/js?id=G-W66CHGTB05"></script> <script>function gtag(){window.dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-W66CHGTB05");</script> <script type="text/javascript">function progressBarSetup(){"max"in document.createElement("progress")?(initializeProgressElement(),$(document).on("scroll",function(){progressBar.attr({value:getCurrentScrollPosition()})}),$(window).on("resize",initializeProgressElement)):(resizeProgressBar(),$(document).on("scroll",resizeProgressBar),$(window).on("resize",resizeProgressBar))}function getCurrentScrollPosition(){return $(window).scrollTop()}function initializeProgressElement(){let e=$("#navbar").outerHeight(!0);$("body").css({"padding-top":e}),$("progress-container").css({"padding-top":e}),progressBar.css({top:e}),progressBar.attr({max:getDistanceToScroll(),value:getCurrentScrollPosition()})}function getDistanceToScroll(){return $(document).height()-$(window).height()}function resizeProgressBar(){progressBar.css({width:getWidthPercentage()+"%"})}function getWidthPercentage(){return getCurrentScrollPosition()/getDistanceToScroll()*100}const progressBar=$("#progress");window.onload=function(){setTimeout(progressBarSetup,50)};</script> <div class="hiddendiv common"></div><iframe id="LgIoc6pK" frameborder="0" src="chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/translateSandbox/translateSandbox.html" style="width: 0px; height: 0px; display: none;"></iframe></body><grammarly-desktop-integration data-grammarly-shadow-root="true"></grammarly-desktop-integration></html>
+"""
+
+def extract_data(html: str) -> dict:
+ from bs4 import BeautifulSoup
+ import re
+
+ # Parse the HTML content using BeautifulSoup
+ soup = BeautifulSoup(html, 'html.parser')
+
+ # Initialize the list to hold project data
+ projects = []
+
+ # Find all anchor tags that contain project information
+ for a_tag in soup.find_all('a', href=True):
+ # Extract the text content of the anchor tag
+ text_content = a_tag.get_text(strip=True)
+
+ # Use regex to split the text content into title and description
+ match = re.match(r'(.+?)\s(.+)', text_content)
+ if match:
+ title = match.group(1)
+ description = match.group(2)
+
+ # Append the project data to the list
+ projects.append({
+ 'title': title,
+ 'description': description
+ })
+
+ # Structure the data according to the specified JSON schema
+ result = {
+ 'title': 'Projects',
+ 'type': 'object',
+ 'properties': {
+ 'projects': {
+ 'title': 'Projects',
+ 'type': 'array',
+ 'items': projects
+ }
+ },
+ 'required': ['projects'],
+ 'definitions': {
+ 'Project': {
+ 'title': 'Project',
+ 'type': 'object',
+ 'properties': {
+ 'title': {
+ 'title': 'Title',
+ 'description': 'The title of the project',
+ 'type': 'string'
+ },
+ 'description': {
+ 'title': 'Description',
+ 'description': 'The description of the project',
+ 'type': 'string'
+ }
+ },
+ 'required': ['title', 'description']
+ }
+ }
+ }
+
+ return result
+
+def create_sandbox_and_execute(function_code, html_doc):
+ # Create a sandbox environment
+ sandbox_globals = {
+ 'BeautifulSoup': BeautifulSoup,
+ 're': re,
+ '__builtins__': __builtins__,
+ }
+
+ # Capture stdout
+ old_stdout = sys.stdout
+ sys.stdout = StringIO()
+
+ try:
+ # Execute the extract_data function with the provided HTML
+ result = extract_data(html_doc)
+
+ return True, result
+ except Exception as e:
+ return False, f"Error during execution: {str(e)}"
+ finally:
+ # Restore stdout
+ sys.stdout = old_stdout
+
+
+#execution_success, execution_result = create_sandbox_and_execute(generated_code, html)
+from langchain_core.pydantic_v1 import BaseModel, Field
+from typing import List
+
+class Project(BaseModel):
+ title: str = Field(description="The title of the project")
+ description: str = Field(description="The description of the project")
+
+class Projects(BaseModel):
+ projects: List[Project]
+
+
+def transform_schema(pydantic_schema):
+ def process_properties(properties):
+ result = {}
+ for key, value in properties.items():
+ if 'type' in value:
+ if value['type'] == 'array':
+ if '$ref' in value['items']:
+ ref_key = value['items']['$ref'].split('/')[-1]
+ result[key] = [process_properties(pydantic_schema['definitions'][ref_key]['properties'])]
+ else:
+ result[key] = [value['items']['type']]
+ else:
+ result[key] = {
+ "type": value['type'],
+ "description": value.get('description', '')
+ }
+ elif '$ref' in value:
+ ref_key = value['$ref'].split('/')[-1]
+ result[key] = process_properties(pydantic_schema['definitions'][ref_key]['properties'])
+ return result
+
+ return process_properties(pydantic_schema['properties'])
+
+
+data_schema = Projects.schema()
+transformed_schema = transform_schema(data_schema)
+print(transformed_schema)
\ No newline at end of file
diff --git a/scrapegraphai/graphs/code_generator_graph.py b/scrapegraphai/graphs/code_generator_graph.py
index c4fd6d7a..6dc769b0 100644
--- a/scrapegraphai/graphs/code_generator_graph.py
+++ b/scrapegraphai/graphs/code_generator_graph.py
@@ -105,25 +105,25 @@ class CodeGeneratorGraph(AbstractGraph):
)
html_analyzer_node = HtmlAnalyzerNode(
- input="refined_prompt & doc",
- output=["html_info"],
+ input="refined_prompt & original_html",
+ output=["html_info", "reduced_html"],
node_config={
"llm_model": self.llm_model,
"additional_info": self.config.get("additional_info"),
- "schema": self.schema
+ "schema": self.schema,
+ "reduction": self.config.get("reduction", 0)
}
)
generate_code_node = GenerateCodeNode(
- input="user_prompt & refined_prompt & html_info & doc & answer",
+ input="user_prompt & refined_prompt & html_info & reduced_html & answer",
output=["generated_code"],
node_config={
+ "library": self.library,
"llm_model": self.llm_model,
"additional_info": self.config.get("additional_info"),
"schema": self.schema
- },
- library=self.library,
- website=self.source
+ }
)
return BaseGraph(
@@ -139,7 +139,7 @@ class CodeGeneratorGraph(AbstractGraph):
(fetch_node, parse_node),
(parse_node, generate_validation_answer_node),
(generate_validation_answer_node, prompt_refier_node),
- (prompt_refier_node, html_analyzer_node)
+ (prompt_refier_node, html_analyzer_node),
(html_analyzer_node, generate_code_node)
],
entry_point=fetch_node,
@@ -157,4 +157,4 @@ class CodeGeneratorGraph(AbstractGraph):
inputs = {"user_prompt": self.prompt, self.input_key: self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
- return self.final_state.get("code", "No code created.")
+ return self.final_state.get("generated_code", "No code created.")
diff --git a/scrapegraphai/nodes/generate_code_node.py b/scrapegraphai/nodes/generate_code_node.py
index b3bb8288..3f73ede2 100644
--- a/scrapegraphai/nodes/generate_code_node.py
+++ b/scrapegraphai/nodes/generate_code_node.py
@@ -17,6 +17,8 @@ import re
from tqdm import tqdm
from .base_node import BaseNode
from pydantic import ValidationError
+from ..utils import transform_schema
+from jsonschema import validate, ValidationError
class GenerateCodeNode(BaseNode):
@@ -126,20 +128,21 @@ class GenerateCodeNode(BaseNode):
user_prompt = input_data[0] # get user prompt
refined_prompt = input_data[1] # get refined prompt
html_info = input_data[2] # get html analysis
- doc = input_data[3] # get html code
+ reduced_html = input_data[3] # get html code
answer = input_data[4] # get answer generated from the generate answer node for verification
if self.node_config.get("schema", None) is not None:
- self.output_schema = self.node_config["schema"] # get JSON output schema
+ self.output_schema = self.node_config["schema"].schema() # get JSON output schema
+ self.simplefied_schema = transform_schema(self.output_schema) # get JSON output schema
prompt = PromptTemplate(
template=template_code_generator,
partial_variables={
"user_input": user_prompt,
- "json_schema": self.output_schema.schema,
+ "json_schema": str(self.simplefied_schema),
"initial_analysis": refined_prompt,
- "html_code": doc,
+ "html_code": reduced_html,
"html_analysis": html_info
})
@@ -150,6 +153,7 @@ class GenerateCodeNode(BaseNode):
# syntax check
print("\Checking code syntax...")
+ generated_code = self.extract_code(generated_code)
syntax_valid, syntax_message = self.syntax_check(generated_code)
if not syntax_valid:
@@ -157,7 +161,7 @@ class GenerateCodeNode(BaseNode):
# code execution
print("\nExecuting code in sandbox...")
- execution_success, execution_result = self.create_sandbox_and_execute(generated_code, doc)
+ execution_success, execution_result = self.create_sandbox_and_execute(generated_code, reduced_html)
if not execution_success:
print(f"Executio failed: {execution_result}")
@@ -180,7 +184,7 @@ class GenerateCodeNode(BaseNode):
except SyntaxError as e:
return False, f"Syntax error: {str(e)}"
- def create_sandbox_and_execute(function_code, html_doc):
+ def create_sandbox_and_execute(self, function_code, html_doc):
# Create a sandbox environment
sandbox_globals = {
'BeautifulSoup': BeautifulSoup,
@@ -214,8 +218,18 @@ class GenerateCodeNode(BaseNode):
def validate_dict(self, data: dict, schema):
try:
- schema(**data) # Use the provided schema directly
+ validate(instance=data, schema=schema)
return True, None
except ValidationError as e:
errors = e.errors()
- return False, errors
\ No newline at end of file
+ return False, errors
+
+ def extract_code(self, code: str) -> str:
+ # Pattern to match the code inside a code block
+ pattern = r'```(?:python)?\n(.*?)```'
+
+ # Search for the code block, if present
+ match = re.search(pattern, code, re.DOTALL)
+
+ # If a code block is found, return the code, otherwise return the entire string
+ return match.group(1) if match else code
\ No newline at end of file
diff --git a/scrapegraphai/nodes/html_analyzer_node.py b/scrapegraphai/nodes/html_analyzer_node.py
index 26f8fb17..cc8b4106 100644
--- a/scrapegraphai/nodes/html_analyzer_node.py
+++ b/scrapegraphai/nodes/html_analyzer_node.py
@@ -11,6 +11,7 @@ from langchain_mistralai import ChatMistralAI
from langchain_community.chat_models import ChatOllama
from tqdm import tqdm
from .base_node import BaseNode
+from ..utils import reduce_html
class HtmlAnalyzerNode(BaseNode):
@@ -100,7 +101,9 @@ class HtmlAnalyzerNode(BaseNode):
This HTML analysis will be used to guide the final code generation process for a function that extracts data from the given HTML string.
Please provide only the analysis with relevant, specific information based on this HTML code. Avoid vague statements and focus on exact details needed for accurate data extraction.
- **Response**:
+ Focus on providing a concise, step-by-step analysis of the HTML structure and the key elements needed for data extraction. Do not include any code examples or implementation logic. Keep the response focused and avoid general statements.**
+
+ **HTML Analysis for Data Extraction**:
"""
template_html_analysis_with_context = """
@@ -133,7 +136,9 @@ class HtmlAnalyzerNode(BaseNode):
This HTML analysis will be used to guide the final code generation process for a function that extracts data from the given HTML string.
Please provide only the analysis with relevant, specific information based on this HTML code. Avoid vague statements and focus on exact details needed for accurate data extraction.
- **Response**:
+ Focus on providing a concise, step-by-step analysis of the HTML structure and the key elements needed for data extraction. Do not include any code examples or implementation logic. Keep the response focused and avoid general statements.**
+
+ **HTML Analysis for Data Extraction**:
"""
self.logger.info(f"--- Executing {self.node_name} Node ---")
@@ -142,25 +147,27 @@ class HtmlAnalyzerNode(BaseNode):
input_data = [state[key] for key in input_keys]
refined_prompt = input_data[0] # get refined user prompt
- doc = input_data[1] # get HTML code
-
+ html = input_data[1] # get HTML code
+
+ reduced_html = reduce_html(html[0].page_content, self.node_config.get("reduction", 0)) # reduce HTML code
+
if self.additional_info is not None: # use additional context if present
prompt = PromptTemplate(
template=template_html_analysis_with_context,
partial_variables={"initial_analysis": refined_prompt,
- "html_code": doc,
+ "html_code": reduced_html,
"additional_context": self.additional_info})
else:
prompt = PromptTemplate(
template=template_html_analysis,
partial_variables={"initial_analysis": refined_prompt,
- "html_code": doc})
+ "html_code": reduced_html})
output_parser = StrOutputParser()
chain = prompt | self.llm_model | output_parser
html_analysis = chain.invoke({})
- state.update({self.output[0]: html_analysis})
+ state.update({self.output[0]: html_analysis, self.output[1]: reduced_html})
return state
diff --git a/scrapegraphai/nodes/prompt_refiner_node.py b/scrapegraphai/nodes/prompt_refiner_node.py
index f9006f15..5aa93ba0 100644
--- a/scrapegraphai/nodes/prompt_refiner_node.py
+++ b/scrapegraphai/nodes/prompt_refiner_node.py
@@ -11,6 +11,7 @@ from langchain_mistralai import ChatMistralAI
from langchain_community.chat_models import ChatOllama
from tqdm import tqdm
from .base_node import BaseNode
+from ..utils import transform_schema
class PromptRefinerNode(BaseNode):
@@ -76,7 +77,7 @@ class PromptRefinerNode(BaseNode):
"""
template_prompt_builder = """
- **Task**: Analyze the user's request and the desired output schema to create a structured description for web scraping. Carefully examine both the user's request and the JSON schema to understand the desired data elements and their relationships.
+ **Task**: Analyze the user's request and the provided JSON schema to clearly map the desired data extraction. Break down the user's request into key components, and then explicitly connect these components to the corresponding elements within the JSON schema.
**User's Request**:
{user_input}
@@ -87,7 +88,14 @@ class PromptRefinerNode(BaseNode):
```
**Analysis Instructions**:
- Genarate the breakdown of the user request and link the elements of the user's request with the json schema
+ 1. **Break Down User Request:**
+ * Clearly identify the core entities or data types the user is asking for.
+ * Highlight any specific attributes or relationships mentioned in the request.
+
+ 2. **Map to JSON Schema**:
+ * For each identified element in the user request, pinpoint its exact counterpart in the JSON schema.
+ * Explain how the schema structure accommodates the user's needs.
+ * If applicable, mention any schema elements that are not directly addressed in the user's request.
This analysis will be used to guide the HTML structure examination and ultimately inform the code generation process.
Please generate only the analysis and no other text.
@@ -96,8 +104,8 @@ class PromptRefinerNode(BaseNode):
"""
template_prompt_builder_with_context = """
- **Task**: Analyze the user's request, the desired output schema, and the additional context the user provided to create a structured description for web scraping. Carefully examine both the user's request and the JSON schema to understand the desired data elements and their relationships.
-
+ **Task**: Analyze the user's request, the provided JSON schema, and the additional context the user provided to clearly map the desired data extraction. Break down the user's request into key components, and then explicitly connect these components to the corresponding elements within the JSON schema.
+
**User's Request**:
{user_input}
@@ -110,7 +118,14 @@ class PromptRefinerNode(BaseNode):
{additional_context}
**Analysis Instructions**:
- Genarate the breakdown of the user request and link the elements of the user's request with the json schema
+ 1. **Break Down User Request:**
+ * Clearly identify the core entities or data types the user is asking for.
+ * Highlight any specific attributes or relationships mentioned in the request.
+
+ 2. **Map to JSON Schema**:
+ * For each identified element in the user request, pinpoint its exact counterpart in the JSON schema.
+ * Explain how the schema structure accommodates the user's needs.
+ * If applicable, mention any schema elements that are not directly addressed in the user's request.
This analysis will be used to guide the HTML structure examination and ultimately inform the code generation process.
Please generate only the analysis and no other text.
@@ -120,26 +135,23 @@ class PromptRefinerNode(BaseNode):
self.logger.info(f"--- Executing {self.node_name} Node ---")
- input_keys = self.get_input_keys(state)
-
- input_data = [state[key] for key in input_keys]
- user_prompt = input_data[0] # get user prompt
+ user_prompt = state['user_prompt'] # get user prompt
if self.node_config.get("schema", None) is not None:
- self.data_schema = self.node_config["schema"] # get JSON schema
+ self.simplefied_schema = transform_schema(self.node_config["schema"].schema()) # get JSON schema
- if self.additional_info is not None: # use additional context if present
+ if self.additional_info is not None: # use additional context if present
prompt = PromptTemplate(
template=template_prompt_builder_with_context,
partial_variables={"user_input": user_prompt,
- "json_schema": self.data_schema.schema(),
+ "json_schema": str(self.simplefied_schema),
"additional_context": self.additional_info})
else:
prompt = PromptTemplate(
template=template_prompt_builder,
partial_variables={"user_input": user_prompt,
- "json_schema": self.schema.schema()})
+ "json_schema": str(self.simplefied_schema)})
output_parser = StrOutputParser()
diff --git a/scrapegraphai/utils/__init__.py b/scrapegraphai/utils/__init__.py
index fbd03800..feffa683 100644
--- a/scrapegraphai/utils/__init__.py
+++ b/scrapegraphai/utils/__init__.py
@@ -7,7 +7,7 @@ from .prettify_exec_info import prettify_exec_info
from .proxy_rotation import Proxy, parse_or_search_proxy, search_proxy_servers
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 .cleanup_html import cleanup_html, reduce_html
from .logging import *
from .convert_to_md import convert_to_md
from .screenshot_scraping.screenshot_preparation import (take_screenshot,
@@ -17,3 +17,4 @@ from .screenshot_scraping.screenshot_preparation import (take_screenshot,
from .screenshot_scraping.text_detection import detect_text
from .tokenizer import num_tokens_calculus
from .split_text_into_chunks import split_text_into_chunks
+from .schema_trasform import transform_schema
\ No newline at end of file
diff --git a/scrapegraphai/utils/cleanup_html.py b/scrapegraphai/utils/cleanup_html.py
index 6c7c3c4c..1521fe01 100644
--- a/scrapegraphai/utils/cleanup_html.py
+++ b/scrapegraphai/utils/cleanup_html.py
@@ -2,7 +2,8 @@
Module for minimizing the code
"""
from urllib.parse import urljoin
-from bs4 import BeautifulSoup
+import re
+from bs4 import BeautifulSoup, Comment
from minify_html import minify
def cleanup_html(html_content: str, base_url: str) -> str:
@@ -53,3 +54,82 @@ def cleanup_html(html_content: str, base_url: str) -> str:
else:
raise ValueError(f"""No HTML body content found, please try setting the 'headless'
flag to False in the graph configuration. HTML content: {html_content}""")
+
+
+def minify_html(html):
+ # Remove comments
+ html = re.sub(r'<!--.*?-->', '', html, flags=re.DOTALL)
+
+ # Remove whitespace between tags
+ html = re.sub(r'>\s+<', '><', html)
+
+ # Remove whitespace at the beginning and end of tags
+ html = re.sub(r'\s+>', '>', html)
+ html = re.sub(r'<\s+', '<', html)
+
+ # Collapse multiple whitespace characters into a single space
+ html = re.sub(r'\s+', ' ', html)
+
+ # Remove spaces around equals signs in attributes
+ html = re.sub(r'\s*=\s*', '=', html)
+
+ return html.strip()
+
+def reduce_html(html, reduction):
+ """
+ Reduces the size of the HTML content based on the specified level of reduction.
+
+ Args:
+ html (str): The HTML content to reduce.
+ reduction (int): The level of reduction to apply to the HTML content.
+ 0: minification only,
+ 1: minification and removig unnecessary tags and attributes,
+ 2: minification, removig unnecessary tags and attributes, simplifying text content, removing of the head tag
+
+ Returns:
+ str: The reduced HTML content based on the specified reduction level.
+ """
+ if reduction == 0:
+ return minify_html(html)
+
+ soup = BeautifulSoup(html, 'html.parser')
+
+ # Remove comments
+ for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
+ comment.extract()
+
+ # Remove script and style tag contents, but keep the tags
+ for tag in soup(['script', 'style']):
+ tag.string = ""
+
+ # Remove unnecessary attributes, but keep class and id
+ attrs_to_keep = ['class', 'id', 'href', 'src']
+ for tag in soup.find_all(True):
+ for attr in list(tag.attrs):
+ if attr not in attrs_to_keep:
+ del tag[attr]
+
+ if reduction == 1:
+ return minify_html(str(soup))
+
+ # Remove script and style tags completely
+ for tag in soup(['script', 'style']):
+ tag.decompose()
+
+ # Focus only on the body
+ body = soup.body
+ if not body:
+ return "No <body> tag found in the HTML"
+
+ # Simplify text content
+ for tag in body.find_all(string=True):
+ if tag.parent.name not in ['script', 'style']:
+ tag.replace_with(re.sub(r'\s+', ' ', tag.strip())[:20])
+
+ # Generate reduced HTML
+ reduced_html = str(body)
+
+ # Apply minification
+ reduced_html = minify_html(reduced_html)
+
+ return reduced_html
\ No newline at end of file
diff --git a/scrapegraphai/utils/schema_trasform.py b/scrapegraphai/utils/schema_trasform.py
new file mode 100644
index 00000000..af752470
--- /dev/null
+++ b/scrapegraphai/utils/schema_trasform.py
@@ -0,0 +1,36 @@
+"""
+This utility function trasfrom the pydantic schema into a more comprehensible schema.
+"""
+
+def transform_schema(pydantic_schema):
+ """
+ Transform the pydantic schema into a more comprehensible JSON schema.
+
+ Args:
+ pydantic_schema (dict): The pydantic schema.
+
+ Returns:
+ dict: The transformed JSON schema.
+ """
+
+ def process_properties(properties):
+ result = {}
+ for key, value in properties.items():
+ if 'type' in value:
+ if value['type'] == 'array':
+ if '$ref' in value['items']:
+ ref_key = value['items']['$ref'].split('/')[-1]
+ result[key] = [process_properties(pydantic_schema['definitions'][ref_key]['properties'])]
+ else:
+ result[key] = [value['items']['type']]
+ else:
+ result[key] = {
+ "type": value['type'],
+ "description": value.get('description', '')
+ }
+ elif '$ref' in value:
+ ref_key = value['$ref'].split('/')[-1]
+ result[key] = process_properties(pydantic_schema['definitions'][ref_key]['properties'])
+ return result
+
+ return process_properties(pydantic_schema['properties'])
\ No newline at end of file