Notebooks
S
ScrapeGraphAI
Scrapegraph Langchain

Scrapegraph Langchain

scrapegraph-pysdk-nodejscookbookscrapingjson-schemagithub-trendingsdk-pythonweb-scraping-pythonweb-scrapingsdk-jsapiPythonscrapegraphweb-crawler

๐Ÿ•ท๏ธ๐Ÿฆœ Extract Github Trending Repositories with langchain-scrapegraph

Presentazione senza titolo.pptx (2).png

๐Ÿ”ง Install dependencies

[ ]

๐Ÿ”‘ Import ScrapeGraph API key

You can find the Scrapegraph API key here

[ ]
SGAI_API_KEY not found in environment.
Please enter your SGAI_API_KEY: ยทยทยทยทยทยทยทยทยทยท
SGAI_API_KEY has been set in the environment.

๐Ÿ“ Defining an Output Schema for Webpage Content Extraction

If you already know what you want to extract from a webpage, you can define an output schema using Pydantic. This schema acts as a "blueprint" that tells the AI how to structure the response.

Pydantic Schema Quick Guide

Types of Schemas

  1. Simple Schema
    Use this when you want to extract straightforward information, such as a single piece of content.
	from pydantic import BaseModel, Field

# Simple schema for a single webpage
class PageInfoSchema(BaseModel):
    title: str = Field(description="The title of the webpage")
    description: str = Field(description="The description of the webpage")

# Example Output JSON after AI extraction
{
    "title": "ScrapeGraphAI: The Best Content Extraction Tool",
    "description": "ScrapeGraphAI provides powerful tools for structured content extraction from websites."
}

  1. Complex Schema (Nested)
    If you need to extract structured information with multiple related items (like a list of repositories), you can nest schemas.
	from pydantic import BaseModel, Field
from typing import List

# Define a schema for a single repository
class RepositorySchema(BaseModel):
    name: str = Field(description="Name of the repository (e.g., 'owner/repo')")
    description: str = Field(description="Description of the repository")
    stars: int = Field(description="Star count of the repository")
    forks: int = Field(description="Fork count of the repository")
    today_stars: int = Field(description="Stars gained today")
    language: str = Field(description="Programming language used")

# Define a schema for a list of repositories
class ListRepositoriesSchema(BaseModel):
    repositories: List[RepositorySchema] = Field(description="List of GitHub trending repositories")

# Example Output JSON after AI extraction
{
    "repositories": [
        {
            "name": "google-gemini/cookbook",
            "description": "Examples and guides for using the Gemini API",
            "stars": 8036,
            "forks": 1001,
            "today_stars": 649,
            "language": "Jupyter Notebook"
        },
        {
            "name": "TEN-framework/TEN-Agent",
            "description": "TEN Agent is a conversational AI powered by TEN, integrating Gemini 2.0 Multimodal Live API, OpenAI Realtime API, RTC, and more.",
            "stars": 3224,
            "forks": 311,
            "today_stars": 361,
            "language": "Python"
        }
    ]
}

Key Takeaways

  • Simple Schema: Perfect for small, straightforward extractions.
  • Complex Schema: Use nesting to extract lists or structured data, like "a list of repositories."

Both approaches give the AI a clear structure to follow, ensuring that the extracted content matches exactly what you need.

[ ]

๐Ÿš€ Initialize langchain-scrapegraph tools and start extraction

Here we use SmartScraperTool to extract structured data using AI from a webpage.

If you already have an HTML file, you can upload it and use LocalScraperTool instead.

You can find more info in the official langchain documentation

[ ]

Invoke the tool

[ ]

As you may have noticed, we are not passing the llm_output_schema while invoking the tool, this will make life easier to AI agents since they will not need to generate one themselves with high risk of failure. Instead, we force the tool to return always a structured output that follows your previously defined schema. To find out more, check the following README

Print the response

[ ]
Trending Repositories:
{
  "repositories": [
    {
      "name": "XiaoMi/ha_xiaomi_home",
      "description": "Xiaomi Home Integration for Home Assistant",
      "stars": 11097,
      "forks": 472,
      "today_stars": 3023,
      "language": "Python"
    },
    {
      "name": "comet-ml/opik",
      "description": "Open-source end-to-end LLM Development Platform",
      "stars": 2741,
      "forks": 169,
      "today_stars": 91,
      "language": "Java"
    },
    {
      "name": "EbookFoundation/free-programming-books",
      "description": "\ud83d\udcda Freely available programming books",
      "stars": 341919,
      "forks": 62038,
      "today_stars": 225,
      "language": "HTML"
    },
    {
      "name": "konfig-dev/konfig",
      "description": "Sunset as of December 2024",
      "stars": 689,
      "forks": 192,
      "today_stars": 224,
      "language": "TypeScript"
    },
    {
      "name": "anoma/anoma",
      "description": "Reference implementation of Anoma",
      "stars": 9451,
      "forks": 452,
      "today_stars": 4129,
      "language": "Elixir"
    },
    {
      "name": "stripe/stripe-ios",
      "description": "Stripe iOS SDK",
      "stars": 2292,
      "forks": 1004,
      "today_stars": 49,
      "language": "Swift"
    },
    {
      "name": "Guovin/iptv-api",
      "description": "IPTV live TV source update tool",
      "stars": 9385,
      "forks": 2010,
      "today_stars": 91,
      "language": "Python"
    },
    {
      "name": "facebookresearch/AnimatedDrawings",
      "description": "Code to accompany \"A Method for Animating Children's Drawings of the Human Figure\"",
      "stars": 11473,
      "forks": 988,
      "today_stars": 398,
      "language": "Python"
    },
    {
      "name": "apache/airflow",
      "description": "Apache Airflow - A platform to programmatically author, schedule, and monitor workflows",
      "stars": 37690,
      "forks": 14411,
      "today_stars": 25,
      "language": "Python"
    },
    {
      "name": "seleniumbase/SeleniumBase",
      "description": "Python APIs for web automation, testing, and bypassing bot-detection.",
      "stars": 6646,
      "forks": 1028,
      "today_stars": 624,
      "language": "Python"
    }
  ]
}

๐Ÿ’พ Save the output to a CSV file

Let's create a pandas dataframe and show the table with the extracted content

[ ]

Save it to CSV

[ ]
Data saved to trending_repositories.csv

๐Ÿ”— Resources

ScrapeGraph API Banner

Made with โค๏ธ by the ScrapeGraphAI Team