Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
refactoring of the functions
VinciGit000bc7e6107270
diff --git a/README.md b/README.mdindex 39746b01..bfee31d9 100644--- a/README.md+++ b/README.md@@ -9,6 +9,8 @@ The use of this library allows to scrape and extract informations from websitesThis library can work passing as a parameter from the code the HTML to scrape or it can work passing thelink of the website that you want to extract informations.+Official documentation page: [https://amazscraper.readthedocs.io/en/latest/index.html](https://amazscraper.readthedocs.io/en/latest/index.html)+# 🔍 DemoTry out AmazScraper in your browser:@@ -66,7 +68,7 @@ python -m AmazScraper.examples.html_scraping```pythonimport osfrom dotenv import load_dotenv-from utils.getter import scraper+from utils.getter import removerfrom utils.class_generator import Generatorload_dotenv()@@ -85,7 +87,7 @@ if __name__ == "__main__":generator_instance = Generator(values, MY_ENV_VAR, 0, "gpt-3.5-turbo")- res = generator_instance.invocation(scraper("https://www.mockupworld.co", 4197))+ res = generator_instance.invocation(remover("https://www.mockupworld.co", 4197))print(res)```diff --git a/amazscraper/class_creator.py b/amazscraper/class_creator.pyindex 312512a0..0439ccd6 100644--- a/amazscraper/class_creator.py+++ b/amazscraper/class_creator.py@@ -13,5 +13,5 @@ def create_class(data_dict: dict):global base_scriptbase_script = base_script + f" {elem['title']}: {elem['type']} = Field(description='{elem['description']}')\n"- with open("./utils/pydantic_class.py", "w") as f:+ with open("./amazscraper/pydantic_class.py", "w") as f:f.write(base_script)\ No newline at end of filediff --git a/amazscraper/getter.py b/amazscraper/getter.pyindex 1c1b509d..1699e8b6 100644--- a/amazscraper/getter.py+++ b/amazscraper/getter.py@@ -18,31 +18,32 @@ def get_function(link:str, param = HEADERS) -> str:response = requests.get(url=link, headers=param)return str(response.content)-def scraper(link: str, max_char: int) -> str:- """- Scrapes the HTML text and removes unwanted elements, text, and comments.-- Args:- link (str): The HTML link to be scraped.- max_char (int): The maximum number of characters in the returned HTML body.+def remover(file:str) -> str:+ '''+ This function elaborate the HTML file and remove all the not necessary tag+ Parameters:+ file (str): the file to parseReturns:- str: The scraped HTML body as a string without script meta tags and limited to max_char characters.- """- text = get_function(link)- soup = BeautifulSoup(text, 'html.parser')+ str: the parsed file+ '''+ res = ""++ isBody = False- unwanted_elements = ['head', 'script', 'style']- unwanted_text = "Per discutere l'accesso automatizzato ai dati di Amazon"- unwanted_comment = "Correios.DoNotSend"+ for elem in file.splitlines():+ if "<title>" in elem:+ res = res + elem- for element in soup(unwanted_elements):- element.decompose()+ if "<body>" in elem:+ isBody = True- for unwanted_content in soup.find_all(string=lambda text: unwanted_text in text or unwanted_comment in text):- unwanted_content.extract()+ if "</body>" in elem:+ break- html_body = str(soup.body).replace('\n', '')+ if "<script>" in elem:+ continue- # Limit the number of characters in the HTML body- return html_body[:max_char]+ if isBody == True:+ res = res + elem+ return resdiff --git a/examples/value_scraping.py b/examples/value_scraping.pyindex c62ca2c4..c835114e 100644--- a/examples/value_scraping.py+++ b/examples/value_scraping.py@@ -1,6 +1,6 @@import osfrom dotenv import load_dotenv-from amazscraper.getter import scraper+from amazscraper.getter import removerfrom amazscraper.class_generator import Generatorload_dotenv()@@ -19,6 +19,6 @@ if __name__ == "__main__":generator_instance = Generator(values, MY_ENV_VAR, 0, "gpt-3.5-turbo")- res = generator_instance.invocation(scraper("https://www.mockupworld.co", 4197))+ res = generator_instance.invocation(remover("https://www.mockupworld.co"))print(res)\ No newline at end of file