|
| 1 | +""" |
| 2 | +Basic example of scraping pipeline using SmartScraper while setting an API rate limit. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +from scrapegraphai.graphs import SmartScraperGraph |
| 8 | +from scrapegraphai.utils import prettify_exec_info |
| 9 | + |
| 10 | + |
| 11 | +# required environment variables in .env |
| 12 | +# ANTHROPIC_API_KEY |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +# ************************************************ |
| 16 | +# Create the SmartScraperGraph instance and run it |
| 17 | +# ************************************************ |
| 18 | + |
| 19 | +graph_config = { |
| 20 | + "llm": { |
| 21 | + "api_key": os.getenv("ANTHROPIC_API_KEY"), |
| 22 | + "model": "anthropic/claude-3-haiku-20240307", |
| 23 | + "rate_limit": { |
| 24 | + "requests_per_second": 1 |
| 25 | + } |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +smart_scraper_graph = SmartScraperGraph( |
| 30 | + prompt="""Don't say anything else. Output JSON only. List me all the events, with the following fields: company_name, event_name, event_start_date, event_start_time, |
| 31 | + event_end_date, event_end_time, location, event_mode, event_category, |
| 32 | + third_party_redirect, no_of_days, |
| 33 | + time_in_hours, hosted_or_attending, refreshments_type, |
| 34 | + registration_available, registration_link""", |
| 35 | + # also accepts a string with the already downloaded HTML code |
| 36 | + source="https://www.hmhco.com/event", |
| 37 | + config=graph_config |
| 38 | +) |
| 39 | + |
| 40 | +result = smart_scraper_graph.run() |
| 41 | +print(result) |
| 42 | + |
| 43 | +# ************************************************ |
| 44 | +# Get graph execution info |
| 45 | +# ************************************************ |
| 46 | + |
| 47 | +graph_exec_info = smart_scraper_graph.get_execution_info() |
| 48 | +print(prettify_exec_info(graph_exec_info)) |
0 commit comments