|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using XMLScraperGraph from XML documents |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +from scrapegraphai.graphs import XMLScraperGraph |
| 8 | +from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +# ************************************************ |
| 12 | +# Read the XML file |
| 13 | +# ************************************************ |
| 14 | + |
| 15 | +FILE_NAME = "inputs/books.xml" |
| 16 | +curr_dir = os.path.dirname(os.path.realpath(__file__)) |
| 17 | +file_path = os.path.join(curr_dir, FILE_NAME) |
| 18 | + |
| 19 | +with open(file_path, 'r', encoding="utf-8") as file: |
| 20 | + text = file.read() |
| 21 | + |
| 22 | +# ************************************************ |
| 23 | +# Define the configuration for the graph |
| 24 | +# ************************************************ |
| 25 | + |
| 26 | +gemini_key = os.getenv("GOOGLE_APIKEY") |
| 27 | + |
| 28 | +graph_config = { |
| 29 | + "llm": { |
| 30 | + "api_key": gemini_key, |
| 31 | + "model": "gemini-pro", |
| 32 | + }, |
| 33 | +} |
| 34 | +# ************************************************ |
| 35 | +# Create the XMLScraperGraph instance and run it |
| 36 | +# ************************************************ |
| 37 | + |
| 38 | +xml_scraper_graph = XMLScraperGraph( |
| 39 | + prompt="List me all the authors, title and genres of the books", |
| 40 | + source=text, # Pass the content of the file, not the file object |
| 41 | + config=graph_config |
| 42 | +) |
| 43 | + |
| 44 | +result = xml_scraper_graph.run() |
| 45 | +print(result) |
| 46 | + |
| 47 | +# ************************************************ |
| 48 | +# Get graph execution info |
| 49 | +# ************************************************ |
| 50 | + |
| 51 | +graph_exec_info = xml_scraper_graph.get_execution_info() |
| 52 | +print(prettify_exec_info(graph_exec_info)) |
| 53 | + |
| 54 | +# Save to json or csv |
| 55 | +convert_to_csv(result, "result") |
| 56 | +convert_to_json(result, "result") |
| 57 | + |
0 commit comments