Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
html_reduction script
Matteo Vedovati545970ce5421
diff --git a/scrapegraphai/code_gen/html_reduce.py b/scrapegraphai/code_gen/html_reduce.pynew file mode 100644index 00000000..a25cd5d8--- /dev/null+++ b/scrapegraphai/code_gen/html_reduce.py@@ -0,0 +1,85 @@+import re+from bs4 import BeautifulSoup, Comment+++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):+ """+ html: str, the HTML content to reduce+ reduction: 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+++ """+ 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++# Get string with html from example.html+html = open('example_1.html').read()++reduced_html = reduce_html(html, 2)++# Print the reduced html in result.html+with open('result_1.html', 'w') as f:+ f.write(reduced_html)\ No newline at end of filediff --git a/scrapegraphai/code_gen/script_genarated.py b/scrapegraphai/code_gen/script_genarated.pynew file mode 100644index 00000000..ee2d9fc3--- /dev/null+++ b/scrapegraphai/code_gen/script_genarated.py@@ -0,0 +1,64 @@+from bs4 import BeautifulSoup+import re++def extract_book_info(html_string):+ """+ Extracts book information (title, author, publication date, publisher) from an HTML string using BeautifulSoup.++ Args:+ html_string: The HTML content as a string.++ Returns:+ A dictionary containing the extracted book information in the desired JSON schema format.+ """++ soup = BeautifulSoup(html_string, 'html.parser')++ # Find all book listings+ book_listings = soup.find_all('div', class_='cc-product-list-item')++ books_data = []+ for listing in book_listings:+ # Extract title+ title_elem = listing.find('a', class_='cc-title')+ title = title_elem.text.strip() if title_elem else None++ # Extract author+ author_elem = listing.find('div', class_='cc-author').find('a', class_='cc-author-name')+ author = author_elem.text.strip() if author_elem else None++ # Extract publisher and publication date+ publisher_info_elem = listing.find('span', class_='cc-publisher')+ publisher_info_text = publisher_info_elem.text.strip() if publisher_info_elem else None++ if publisher_info_text:+ # Assuming publisher name is linked and publication date is the remaining text+ publisher_elem = publisher_info_elem.find('a', class_='cc-publisher-name')+ publisher = publisher_elem.text.strip() if publisher_elem else None++ # Use regex to extract year (assuming 4-digit year format)+ publication_date_match = re.search(r'\b(\d{4})\b', publisher_info_text)+ publication_date = publication_date_match.group(1) if publication_date_match else None+ else:+ publisher = None+ publication_date = None++ # Create a book dictionary and append to the list+ book_data = {+ "title": title,+ "author": author,+ "publication_date": publication_date,+ "publisher": publisher+ }+ books_data.append(book_data)++ # Structure the output according to the JSON schema+ output = {+ "books": books_data+ }++ return output++html = open('example_1.html').read()+result = extract_book_info(html)+print(result)\ No newline at end of filediff --git a/scrapegraphai/code_gen/script_runner.py b/scrapegraphai/code_gen/script_runner.pynew file mode 100644index 00000000..e69de29b