Skip to main content

Autonomous Scraping Agent: Scrape URLs with prompts and schema

Project description

AI Tech Crawler - Smart Scraping Agent

GitHub Stars GitHub Forks

PyPI version Python Version Downloads

SmartScrapingAgent is a Python package designed to simplify web scraping using state-of-the-art LLMs (Large Language Models) and customizable schemas. With this package, you can extract structured data efficiently from large, complex web pages.


Installation

Step 1: Install SmartScrapingAgent

Install the Smart Scraping Agent package:

pip install ai-tech-crawler

Step 2: Install Playwright

Install Playwright, which is required for handling headless browsing:

playwright install

Usage

Here is a step-by-step guide to using the Smart Scraping Agent package:

N.B.: This import is required only for jupyter notebooks, since they have their own eventloop

pip install nest-asyncio
import nest_asyncio

nest_asyncio.apply()

Step 1: Import Required Modules

Import necessary modules and classes:

import os, json
from dotenv import load_dotenv


load_dotenv()

Step 2: Define the Configuration

Set up the configuration for the scraping pipeline:

agent_config = {
    "llm": {
        "api_key": os.getenv('OPENAI_API_KEY'),
        "model": "openai/gpt-4o-mini",
        # Uncomment for other models
        # "model": "ollama/nemotron-mini",
        # "device": "cuda",
        # "model_kwargs": {'response_format': {"type": "json_object"}}
    },
    "verbose": True,
    "headless": True,
    "max_retries": 3
}

Step 3: Write Your Prompt

Define a simple prompt to guide the scraping process:

simple_prompt = """
Extract all the trending topics, their search volumes, when it started trending and the trend breakdown from the website's content.
"""

Step 4: Load the Schema

Use the schema to define the structure of the extracted data: Schema can be:

  1. format instruction string with examples
  2. dict
  3. json
  4. pydantic or BaseModel
schema_ = {
    'trends': [
        {
            'topic': 'Trending topic',
            'search_volume': 'Search Volume of a topic',
            'started': 'Time when it started trending',
            'trend_breakdown': 'A trend may consist of multiple queries that are variants of the same search or considered to be related. Trend breakdown details these queries.'
         }
    ],
    'other_links':[
        'list of any other reference URLs'
    ]
}

N.B.: For better results use a valid pydantic schema which is a subclass of BaseModel.

Step 5: Instantiate the SmartScraperAgent

Create an instance of the SmartScraperAgent with the necessary parameters:

from ai_tech_crawler import SmartScraperAgent

url = "https://trends.google.com/trending"

smart_scraper_agent = SmartScraperAgent(
    prompt=simple_prompt,
    source=url,
    config=agent_config,
    schema=schema_
)

Step 6: Run the Scraper

Execute the scraping pipeline and process the results:

result = smart_scraper_agent.run()
print(json.dumps(result, indent=4))

Load a Webpage content as Markdown

markdown_content = smart_scraper_agent.get_markdown()
print(markdown_content)

Load recursive webpages and split it into Documents

documents = smart_scraper_agent.load_indepth_and_split(depth=2)
print(documents[0].page_content)
print(documents[0].metadata)

Key Features

  • LLM-Powered: Leverage advanced models like GPT for smart data extraction.
  • Schema-Driven: Flexible schema design to control output format.
  • Headless Browsing: Playwright integration for efficient, non-visual browsing.
  • Customizable: Fine-tune the pipeline using configurations and custom merge methods.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests on the GitHub repository.


License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_tech_crawler-0.0.4.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_tech_crawler-0.0.4-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file ai_tech_crawler-0.0.4.tar.gz.

File metadata

  • Download URL: ai_tech_crawler-0.0.4.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for ai_tech_crawler-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d85fb2047e637c347e9a743e6475f1f7507a73affb89814059cfeeedd3d307ee
MD5 77191ff44db8b215fc95e966b145dc0b
BLAKE2b-256 e1da0de37f65954ac01864d77b9caba2b36669888a08ab5ea011117698f39ebd

See more details on using hashes here.

File details

Details for the file ai_tech_crawler-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_tech_crawler-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aabef46f571e90e609c70d6e2a2430b47bfa61e1fbe449d1f4b2d9b9f693f244
MD5 d5053cfd7b45f3281e10bedb96decc47
BLAKE2b-256 9f74d3fd15551474785258bcb3b11afda5d997628465d1d3c3a57dc88641fd6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page