|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using SmartScraper from text |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +from scrapegraphai.graphs import ScriptCreatorGraph |
| 8 | +from scrapegraphai.utils import prettify_exec_info |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +# ************************************************ |
| 12 | +# Read the text file |
| 13 | +# ************************************************ |
| 14 | +files = ["inputs/example_1.txt", "inputs/example_2.txt"] |
| 15 | +tasks = ["List me all the projects with their description.", |
| 16 | + "List me all the articles with their description."] |
| 17 | + |
| 18 | +# ************************************************ |
| 19 | +# Define the configuration for the graph |
| 20 | +# ************************************************ |
| 21 | + |
| 22 | +openai_key = os.getenv("GPT4_KEY") |
| 23 | + |
| 24 | + |
| 25 | +graph_config = { |
| 26 | + "llm": { |
| 27 | + "model": "ollama/llama3", |
| 28 | + "temperature": 0, |
| 29 | + # "model_tokens": 2000, # set context length arbitrarily, |
| 30 | + "base_url": "http://localhost:11434", # set ollama URL arbitrarily |
| 31 | + }, |
| 32 | + "embeddings": { |
| 33 | + "model": "ollama/nomic-embed-text", |
| 34 | + "temperature": 0, |
| 35 | + "base_url": "http://localhost:11434", # set ollama URL arbitrarily |
| 36 | + }, |
| 37 | + "library": "beautifoulsoup" |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +# ************************************************ |
| 42 | +# Create the SmartScraperGraph instance and run it |
| 43 | +# ************************************************ |
| 44 | + |
| 45 | +for i in range(0, 2): |
| 46 | + with open(files[i], 'r', encoding="utf-8") as file: |
| 47 | + text = file.read() |
| 48 | + |
| 49 | + smart_scraper_graph = ScriptCreatorGraph( |
| 50 | + prompt=tasks[i], |
| 51 | + source=text, |
| 52 | + config=graph_config |
| 53 | + ) |
| 54 | + |
| 55 | + result = smart_scraper_graph.run() |
| 56 | + print(result) |
| 57 | + # ************************************************ |
| 58 | + # Get graph execution info |
| 59 | + # ************************************************ |
| 60 | + |
| 61 | + graph_exec_info = smart_scraper_graph.get_execution_info() |
| 62 | + print(prettify_exec_info(graph_exec_info)) |
0 commit comments