/Scrapegraph-ai

Python scraper based on AI

Primary LanguagePythonMIT LicenseMIT

πŸ•·οΈ ScrapeGraphAI: You Only Scrape Once

Downloads linting: pylint Pylint CodeQL License: MIT

ScrapeGraphAI is a web scraping python library that uses LLM and direct graph logic to create scraping pipelines for websites and local documents (XML, HTML, JSON, etc.).

Just say which information you want to extract and the library will do it for you!

Scrapegraph-ai Logo

My Skills My Skills My Skills

πŸš€ Quick install

The reference page for Scrapegraph-ai is available on the official page of pypy: pypi.

pip install scrapegraphai

you will also need to install Playwright for javascript-based scraping:

playwright install

Note: it is recommended to install the library in a virtual environment to avoid conflicts with other libraries 🐱

πŸ” Demo

Official streamlit demo:

My Skills

Try it directly on the web using Google Colab:

Open In Colab

Follow the procedure on the following link to setup your OpenAI API key: link.

πŸ“– Documentation

The documentation for ScrapeGraphAI can be found here.

Check out also the docusaurus documentation.

πŸ’» Usage

There are three main scraping pipelines that can be used to extract information from a website (or local file):

  • SmartScraperGraph: single-page scraper that only needs a user prompt and an input source;
  • SearchGraph: multi-page scraper that extracts information from the top n search results of a search engine;
  • SpeechGraph: single-page scraper that extracts information from a website and generates an audio file.

It is possible to use different LLM through APIs, such as OpenAI, Groq, Azure and Gemini, or local models using Ollama.

Case 1: SmartScraper using Local Models

Remember to have Ollama installed and download the models using the ollama pull command.

from scrapegraphai.graphs import SmartScraperGraph

graph_config = {
    "llm": {
        "model": "ollama/mistral",
        "temperature": 0,
        "format": "json",  # Ollama needs the format to be specified explicitly
        "base_url": "http://localhost:11434",  # set Ollama URL
    },
    "embeddings": {
        "model": "ollama/nomic-embed-text",
        "base_url": "http://localhost:11434",  # set Ollama URL
    },
    "verbose": True,
}

smart_scraper_graph = SmartScraperGraph(
    prompt="List me all the projects with their descriptions",
    # also accepts a string with the already downloaded HTML code
    source="https://perinim.github.io/projects",
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

The output will be a list of projects with their descriptions like the following:

{'projects': [{'title': 'Rotary Pendulum RL', 'description': 'Open Source project aimed at controlling a real life rotary pendulum using RL algorithms'}, {'title': 'DQN Implementation from scratch', 'description': 'Developed a Deep Q-Network algorithm to train a simple and double pendulum'}, ...]}

Case 2: SearchGraph using Mixed Models

We use Groq for the LLM and Ollama for the embeddings.

from scrapegraphai.graphs import SearchGraph

# Define the configuration for the graph
graph_config = {
    "llm": {
        "model": "groq/gemma-7b-it",
        "api_key": "GROQ_API_KEY",
        "temperature": 0
    },
    "embeddings": {
        "model": "ollama/nomic-embed-text",
        "base_url": "http://localhost:11434",  # set ollama URL arbitrarily
    },
    "max_results": 5,
}

# Create the SearchGraph instance
search_graph = SearchGraph(
    prompt="List me all the traditional recipes from Chioggia",
    config=graph_config
)

# Run the graph
result = search_graph.run()
print(result)

The output will be a list of recipes like the following:

{'recipes': [{'name': 'Sarde in SaΓ²re'}, {'name': 'Bigoli in salsa'}, {'name': 'Seppie in umido'}, {'name': 'Moleche frite'}, {'name': 'Risotto alla pescatora'}, {'name': 'Broeto'}, {'name': 'Bibarasse in Cassopipa'}, {'name': 'Risi e bisi'}, {'name': 'Smegiassa Ciosota'}]}

Case 3: SpeechGraph using OpenAI

You just need to pass the OpenAI API key and the model name.

from scrapegraphai.graphs import SpeechGraph

graph_config = {
    "llm": {
        "api_key": "OPENAI_API_KEY",
        "model": "gpt-3.5-turbo",
    },
    "tts_model": {
        "api_key": "OPENAI_API_KEY",
        "model": "tts-1",
        "voice": "alloy"
    },
    "output_path": "audio_summary.mp3",
}

# ************************************************
# Create the SpeechGraph instance and run it
# ************************************************

speech_graph = SpeechGraph(
    prompt="Make a detailed audio summary of the projects.",
    source="https://perinim.github.io/projects/",
    config=graph_config,
)

result = speech_graph.run()
print(result)

The output will be an audio file with the summary of the projects on the page.

🀝 Contributing

Feel free to contribute and join our Discord server to discuss with us improvements and give us suggestions!

Please see the contributing guidelines.

πŸ“ˆ Roadmap

Check out the project roadmap here! πŸš€

Wanna visualize the roadmap in a more interactive way? Check out the markmap visualization by copy pasting the markdown content in the editor!

❀️ Contributors

Contributors

Sponsors

SerpAPI

πŸŽ“ Citations

If you have used our library for research purposes please quote us with the following reference:

  @misc{scrapegraph-ai,
    author = {Marco Perini, Lorenzo Padoan, Marco Vinciguerra},
    title = {Scrapegraph-ai},
    year = {2024},
    url = {https://github.com/VinciGit00/Scrapegraph-ai},
    note = {A Python library for scraping leveraging large language models}
  }

Authors

Authors_logos

Contact Info
Marco Vinciguerra Linkedin Badge
Marco Perini Linkedin Badge
Lorenzo Padoan Linkedin Badge

πŸ“œ License

ScrapeGraphAI is licensed under the MIT License. See the LICENSE file for more information.

Acknowledgements

  • We would like to thank all the contributors to the project and the open-source community for their support.
  • ScrapeGraphAI is meant to be used for data exploration and research purposes only. We are not responsible for any misuse of the library.