|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# ScrapingBee\n", |
| 8 | + "*The Best Web Scraping API to Avoid Getting Blocked*\n", |
| 9 | + "\n", |
| 10 | + "## Overview\n", |
| 11 | + "The ScrapingBee web scraping API handles headless browsers, rotates proxies for you, and offers AI-powered data extraction.\n", |
| 12 | + "\n", |
| 13 | + "## Installation\n", |
| 14 | + "\n", |
| 15 | + "```bash\n", |
| 16 | + "pip install -U langchain-scrapingbee\n", |
| 17 | + "```\n", |
| 18 | + "\n", |
| 19 | + "And you should configure credentials by setting the following environment variables:\n", |
| 20 | + "\n", |
| 21 | + "* SCRAPINGBEE_API_KEY\n", |
| 22 | + "\n", |
| 23 | + "You can get your API KEY and 1000 free credits by signing up [here](https://app.scrapingbee.com/account/register).\n", |
| 24 | + "\n", |
| 25 | + "## Tools\n", |
| 26 | + "\n", |
| 27 | + "ScrapingBee Integration provides you acceess to the following tools:\n", |
| 28 | + "\n", |
| 29 | + "* [ScrapeUrlTool](../../../../docs/docs/integrations/tools/scrapingbee_scrapeurl.ipynb) - Scrape the contents of any public website.\n", |
| 30 | + "* [GoogleSearchTool](../../../../docs/docs/integrations/tools/scrapingbee_googlesearch.ipynb) - Search Google to obtain the following types of information regular search (classic), news, maps, and images.\n", |
| 31 | + "* [CheckUsageTool](../../../../docs/docs/integrations/tools/scrapingbee_checkusage.ipynb) — Monitor your ScrapingBee credit or concurrency usage using this tool.\n", |
| 32 | + "\n", |
| 33 | + "## Example" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": { |
| 40 | + "id": "y8ku6X96sebl" |
| 41 | + }, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "import os\n", |
| 45 | + "import getpass\n", |
| 46 | + "from langchain_scrapingbee import (\n", |
| 47 | + " ScrapeUrlTool,\n", |
| 48 | + " GoogleSearchTool,\n", |
| 49 | + " CheckUsageTool,\n", |
| 50 | + ")\n", |
| 51 | + "\n", |
| 52 | + "api_key = os.environ.get(\"SCRAPINGBEE_API_KEY\")\n", |
| 53 | + "if not api_key:\n", |
| 54 | + " print(\n", |
| 55 | + " \"SCRAPINGBEE_API_KEY environment variable is not set. Please enter the API Key here:\"\n", |
| 56 | + " )\n", |
| 57 | + " os.environ[\"SCRAPINGBEE_API_KEY\"] = getpass.getpass()\n", |
| 58 | + "\n", |
| 59 | + "scrape_tool = ScrapeUrlTool(api_key=os.environ.get(\"SCRAPINGBEE_API_KEY\"))\n", |
| 60 | + "search_tool = GoogleSearchTool(api_key=os.environ.get(\"SCRAPINGBEE_API_KEY\"))\n", |
| 61 | + "usage_tool = CheckUsageTool(api_key=os.environ.get(\"SCRAPINGBEE_API_KEY\"))\n", |
| 62 | + "\n", |
| 63 | + "# --- Test Case 1: Scrape a standard HTML page ---\n", |
| 64 | + "print(\"--- 1. Testing ScrapeUrlTool (HTML) ---\")\n", |
| 65 | + "html_result = scrape_tool.invoke({\"url\": \"http://httpbin.org/html\"})\n", |
| 66 | + "print(html_result)\n", |
| 67 | + "\n", |
| 68 | + "\n", |
| 69 | + "# --- Test Case 2: Scrape a PDF file ---\n", |
| 70 | + "print(\"--- 2. Testing ScrapeUrlTool (PDF) ---\")\n", |
| 71 | + "pdf_result = scrape_tool.invoke(\n", |
| 72 | + " {\n", |
| 73 | + " \"url\": \"https://treaties.un.org/doc/publication/ctc/uncharter.pdf\",\n", |
| 74 | + " \"params\": {\"render_js\": False},\n", |
| 75 | + " }\n", |
| 76 | + ")\n", |
| 77 | + "print(pdf_result)\n", |
| 78 | + "\n", |
| 79 | + "\n", |
| 80 | + "# --- Test Case 3: Google Search ---\n", |
| 81 | + "print(\"--- 3. Testing GoogleSearchTool ---\")\n", |
| 82 | + "search_result = search_tool.invoke({\"search\": \"What is LangChain?\"})\n", |
| 83 | + "print(search_result)\n", |
| 84 | + "\n", |
| 85 | + "\n", |
| 86 | + "# --- Test Case 4: Check Usage ---\n", |
| 87 | + "print(\"--- 4. Testing CheckUsageTool ---\")\n", |
| 88 | + "usage_result = usage_tool.invoke({}) # No arguments needed\n", |
| 89 | + "print(usage_result)" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "## Example Using Agent" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": null, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "import os\n", |
| 106 | + "from langchain_scrapingbee import (\n", |
| 107 | + " ScrapeUrlTool,\n", |
| 108 | + " GoogleSearchTool,\n", |
| 109 | + " CheckUsageTool,\n", |
| 110 | + ")\n", |
| 111 | + "from langchain_google_genai import ChatGoogleGenerativeAI\n", |
| 112 | + "from langgraph.prebuilt import create_react_agent\n", |
| 113 | + "\n", |
| 114 | + "if not os.environ.get(\"GOOGLE_API_KEY\") or not os.environ.get(\"SCRAPINGBEE_API_KEY\"):\n", |
| 115 | + " raise ValueError(\n", |
| 116 | + " \"Google and ScrapingBee API keys must be set in environment variables.\"\n", |
| 117 | + " )\n", |
| 118 | + "\n", |
| 119 | + "llm = ChatGoogleGenerativeAI(temperature=0, model=\"gemini-2.5-flash\")\n", |
| 120 | + "scrapingbee_api_key = os.environ.get(\"SCRAPINGBEE_API_KEY\")\n", |
| 121 | + "\n", |
| 122 | + "tools = [\n", |
| 123 | + " ScrapeUrlTool(api_key=scrapingbee_api_key),\n", |
| 124 | + " GoogleSearchTool(api_key=scrapingbee_api_key),\n", |
| 125 | + " CheckUsageTool(api_key=scrapingbee_api_key),\n", |
| 126 | + "]\n", |
| 127 | + "\n", |
| 128 | + "agent = create_react_agent(llm, tools)\n", |
| 129 | + "\n", |
| 130 | + "user_input = (\n", |
| 131 | + " \"If I have enough API Credits, search for pdfs about langchain and save 3 pdfs.\"\n", |
| 132 | + ")\n", |
| 133 | + "\n", |
| 134 | + "# Stream the agent's output step-by-step\n", |
| 135 | + "for step in agent.stream(\n", |
| 136 | + " {\"messages\": user_input},\n", |
| 137 | + " stream_mode=\"values\",\n", |
| 138 | + "):\n", |
| 139 | + " step[\"messages\"][-1].pretty_print()" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "## Documentation\n", |
| 147 | + "* [HTML API](https://www.scrapingbee.com/documentation/)\n", |
| 148 | + "* [Google Search API](https://www.scrapingbee.com/documentation/google/)\n", |
| 149 | + "* [Data Extraction](https://www.scrapingbee.com/documentation/data-extraction/)\n", |
| 150 | + "* [JavaScript Scenario](https://www.scrapingbee.com/documentation/js-scenario/)" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "metadata": { |
| 155 | + "colab": { |
| 156 | + "provenance": [] |
| 157 | + }, |
| 158 | + "kernelspec": { |
| 159 | + "display_name": "Python 3 (ipykernel)", |
| 160 | + "language": "python", |
| 161 | + "name": "python3" |
| 162 | + }, |
| 163 | + "language_info": { |
| 164 | + "codemirror_mode": { |
| 165 | + "name": "ipython", |
| 166 | + "version": 3 |
| 167 | + }, |
| 168 | + "file_extension": ".py", |
| 169 | + "mimetype": "text/x-python", |
| 170 | + "name": "python", |
| 171 | + "nbconvert_exporter": "python", |
| 172 | + "pygments_lexer": "ipython3", |
| 173 | + "version": "3.10.11" |
| 174 | + } |
| 175 | + }, |
| 176 | + "nbformat": 4, |
| 177 | + "nbformat_minor": 1 |
| 178 | +} |
0 commit comments