Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Update parse_node.py
Marco Vinciguerrafc738cacace8
diff --git a/scrapegraphai/nodes/parse_node.py b/scrapegraphai/nodes/parse_node.pyindex bfb825f6..579d34ea 100644--- a/scrapegraphai/nodes/parse_node.py+++ b/scrapegraphai/nodes/parse_node.py@@ -3,14 +3,13 @@ ParseNode Module"""from typing import Tuple, List, Optionalfrom urllib.parse import urljoin+import refrom semchunk import chunkfrom langchain_community.document_transformers import Html2TextTransformerfrom langchain_core.documents import Documentfrom .base_node import BaseNodefrom ..helpers import default_filters-import re-class ParseNode(BaseNode):"""A node responsible for parsing HTML content from a document.@@ -61,14 +60,12 @@ class ParseNode(BaseNode):"""cleaned_urls = []for url in urls:- # Remove any leading 'thumbnail](' or similar patternsurl = re.sub(r'.*?\]\(', '', url)-- # Remove any trailing parentheses or brackets+url = url.rstrip(').')-+cleaned_urls.append(url)-+return cleaned_urlsdef extract_urls(self, text: str, source: str) -> Tuple[List[str], List[str]]:@@ -81,26 +78,21 @@ class ParseNode(BaseNode):Returns:Tuple[List[str], List[str]]: A tuple containing the extracted link URLs and image URLs."""- # Return empty lists if the URLs are not to be parsedif not self.parse_urls:return [], []-- # Regular expression to find URLs (both links and images)+image_extensions = default_filters.filter_dict["img_exts"]image_extension_seq = '|'.join(image_extensions).replace('.','')url_pattern = re.compile(r'(https?://[^\s]+|\S+\.(?:' + image_extension_seq + '))')- # Find all URLs in the stringall_urls = url_pattern.findall(text)all_urls = self._clean_urls(all_urls)if not source.startswith("http"):- # Remove any URLs that is not completeall_urls = [url for url in all_urls if url.startswith("http")]else:- # Add to local URLs the source URLall_urls = [urljoin(source, url) for url in all_urls]-+images = [url for url in all_urls if any(url.endswith(ext) for ext in image_extensions)]links = [url for url in all_urls if url not in images]@@ -136,7 +128,7 @@ class ParseNode(BaseNode):return token_count(text, self.llm_model.model_name)if self.parse_html:- docs_transformed = Html2TextTransformer().transform_documents(input_data[0])+ docs_transformed = Html2TextTransformer(ignore_links=False).transform_documents(input_data[0])docs_transformed = docs_transformed[0]link_urls, img_urls = self.extract_urls(docs_transformed.page_content, source)