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 classes, removed .env in the class + documentation
VinciGit00bad46ffe396e
diff --git a/README.md b/README.mdindex ed7002ab..cde8aa8f 100644--- a/README.md+++ b/README.md@@ -8,7 +8,9 @@ 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.+# π Demo+Try out AmazScraper in your browser:[](https://codespaces.new/VinciGit00/AmazScraper)@@ -61,30 +63,42 @@ python -m AmazScraper.examples.html_scraping# π Examples```python-from AmazScraper.utils.class_generator import Generator+import os+from dotenv import load_dotenv+from utils.getter import scraper+from utils.class_generator import Generator++load_dotenv()-from AmazScraper.utils.getter import get_function, scraper+MY_ENV_VAR = os.getenv('API_KEY')values = [{- "title": "title",+ "title": "title_website","type": "str",- "description": "Title of the items"+ "description": "Give me the website name"}]-if name == "__main__":+if __name__ == "__main__":- generator_instance = Generator(values, 0, "gpt-3.5-turbo")+ generator_instance = Generator(values, MY_ENV_VAR, 0, "gpt-3.5-turbo")res = generator_instance.invocation(scraper("https://www.mockupworld.co", 4197))++ print(res)```### Case 2: Passing your own HTML code```python-import sys-from AmazScraper.utils.class_generator import Generator+import os+from dotenv import load_dotenv+from utils.class_generator import Generator++load_dotenv()++MY_ENV_VAR = os.getenv('API_KEY')values = [{@@ -134,7 +148,7 @@ query_info = '''if __name__ == "__main__":- generator_instance = Generator(values, 0, "gpt-3.5-turbo")+ generator_instance = Generator(values, MY_ENV_VAR, 0, "gpt-3.5-turbo")res = generator_instance.invocation(query_info)print(res)diff --git a/.env.example b/examples/.env.examplesimilarity index 100%rename from .env.examplerename to examples/.env.examplediff --git a/examples/html_scraping.py b/examples/html_scraping.pyindex 1e1efce2..8937c5c4 100644--- a/examples/html_scraping.py+++ b/examples/html_scraping.py@@ -1,6 +1,11 @@-import sys+import os+from dotenv import load_dotenvfrom utils.class_generator import Generator+load_dotenv()++MY_ENV_VAR = os.getenv('API_KEY')+values = [{"title": "title",@@ -49,7 +54,7 @@ query_info = '''if __name__ == "__main__":- generator_instance = Generator(values, 0, "gpt-3.5-turbo")+ generator_instance = Generator(values, MY_ENV_VAR, 0, "gpt-3.5-turbo")res = generator_instance.invocation(query_info)print(res)\ No newline at end of filediff --git a/examples/value_scraping.py b/examples/value_scraping.pynew file mode 100644index 00000000..7c338f5c--- /dev/null+++ b/examples/value_scraping.py@@ -0,0 +1,24 @@+import os+from dotenv import load_dotenv+from utils.getter import scraper+from utils.class_generator import Generator++load_dotenv()++MY_ENV_VAR = os.getenv('API_KEY')++values = [+ {+ "title": "title_website",+ "type": "str",+ "description": "Give me the website name"+ }+]++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))++ print(res)\ No newline at end of filediff --git a/examples/values_scraping.py b/examples/values_scraping.pydeleted file mode 100644index e4a329b9..00000000--- a/examples/values_scraping.py+++ /dev/null@@ -1,19 +0,0 @@-from utils.class_generator import Generator--from utils.getter import get_function, scraper--values = [- {- "title": "title",- "type": "str",- "description": "Title of the items"- }-]--if __name__ == "__main__":-- generator_instance = Generator(values, 0, "gpt-3.5-turbo")-- res = generator_instance.invocation(scraper("https://www.mockupworld.co", 4197))-- print(res)\ No newline at end of filediff --git a/utils/class_creator.py b/utils/class_creator.pyindex d1b1dd3f..312512a0 100644--- a/utils/class_creator.py+++ b/utils/class_creator.py@@ -1,14 +1,17 @@+import time+base_script = '''from langchain_core.pydantic_v1 import BaseModel, Fieldclass _Response(BaseModel):'''--# This function creates a class at runtime using the values from the list.def create_class(data_dict: dict):+ '''+ This function creates a class at runtime using the values from the list.+ '''for elem in data_dict:global base_scriptbase_script = base_script + f" {elem['title']}: {elem['type']} = Field(description='{elem['description']}')\n"- with open("./pydantic_class.py", "w") as f:- f.write(base_script)+ with open("./utils/pydantic_class.py", "w") as f:+ f.write(base_script)\ No newline at end of filediff --git a/utils/class_generator.py b/utils/class_generator.pyindex 92363e3a..22f54e4e 100644--- a/utils/class_generator.py+++ b/utils/class_generator.py@@ -1,4 +1,3 @@-import osfrom dotenv import load_dotenvfrom .pydantic_class import _Responsefrom .class_creator import create_class@@ -7,12 +6,8 @@ from langchain.prompts import PromptTemplatefrom langchain_core.pydantic_v1 import Fieldfrom langchain.output_parsers import PydanticOutputParser-load_dotenv()--MY_ENV_VAR = os.getenv('API_KEY')-class Generator:- def __init__(self, values:list, temperature_param:float = 0, model_name:str = "gpt-3.5-turbo"):+ def __init__(self, values:list, api_key:str, temperature_param:float = 0, model_name:str = "gpt-3.5-turbo"):"""Initializes the Generator object.@@ -35,7 +30,7 @@ class Generator:partial_variables={"format_instructions": self.parser.get_format_instructions()},)- self.model = ChatOpenAI(openai_api_key=MY_ENV_VAR, temperature=temperature_param, model=model_name)+ self.model = ChatOpenAI(openai_api_key=api_key, temperature=temperature_param, model=model_name)self.chain = self.prompt | self.model | self.parserdiff --git a/utils/getter.py b/utils/getter.pyindex e499621f..1c1b509d 100644--- a/utils/getter.py+++ b/utils/getter.py@@ -15,7 +15,7 @@ def get_function(link:str, param = HEADERS) -> str:Returns:str: The content of the response as a string."""- response = requests.get(url=link, headers=HEADERS)+ response = requests.get(url=link, headers=param)return str(response.content)def scraper(link: str, max_char: int) -> str:diff --git a/utils/pydantic_class.py b/utils/pydantic_class.pyindex e9b554d1..8566aa09 100644--- a/utils/pydantic_class.py+++ b/utils/pydantic_class.py@@ -2,4 +2,4 @@from langchain_core.pydantic_v1 import BaseModel, Fieldclass _Response(BaseModel):- title: str = Field(description='Title of the news')+ title_website: str = Field(description='Give me the website name')