AI agent instructions
AI agent instructions
POST /scrape to extract structured data, then save it to MySQL. This example extracts a page title and description and keeps one row per URL. Each successful run updates that row.
Choose where the job runs:
- Notte hosts the schedule and scrape: a Function runs hourly and sends the result to a small HTTPS endpoint in your application, which writes to MySQL.
- Your server runs the whole job: a Python script scrapes and writes directly to MySQL, scheduled with cron.
Hosted Functions do not currently include a MySQL driver. The direct MySQL script below runs on your server, not in the hosted Function runtime. The hosted option uses HTTPS for database delivery.
1. Prepare MySQL
Use Python 3.11+ and a Notte API key. On the server that connects to MySQL, installrequests, pydantic, and PyMySQL:
Use a MySQL server/account that requires TLS. Allow connections from the machine running the writer. The script verifies the server certificate and hostname.
2. Save the scripts
Save this first file asscrape_function.py. Its scrape_page() helper calls the simple scrape endpoint; run() is the entry point for the hosted option.
scrape_function.py
response_format defines the fields to extract. Change PageData, the instructions, and the destination columns together to collect different data. HTTP errors and unsuccessful structured extraction raise an exception before a database write.
Save the writer as scrape_to_mysql.py in the same directory:
scrape_to_mysql.py
scraped_at records the last successful write in UTC.
Option A: Run hourly as a hosted Function
Add an HTTPS receiver
If you already have a backend, add an authenticated endpoint that callssave_page(). Here is a minimal FastAPI receiver. Save it as mysql_receiver.py, alongside the two files above:
mysql_receiver.py
fastapi and uvicorn, set a strong random MYSQL_INGEST_TOKEN on the receiver, and deploy it behind HTTPS using your normal application hosting. For local development, start it with uvicorn mysql_receiver:app --host 127.0.0.1 --port 8000. Hosted Functions must use its publicly reachable HTTPS URL, for example https://your-app.example/ingest, not localhost.
The receiver needs the MySQL variables from step 1. It does not need a Notte API key: the Function performs the scrape. Keep the endpoint authenticated; it can write to your database.
Deploy and test the Function
In the Console, open Settings > Function Secrets and add:MYSQL_INGEST_URL: your receiver’s HTTPS URL, including/ingest.MYSQL_INGEST_TOKEN: the same token configured on the receiver.
notte-sdk locally and set NOTTE_API_KEY, then deploy only scrape_function.py:
deploy_function.py
{"url": "https://example.com", "saved": true} and check the saved row:
Enable the hourly schedule
Open the Function in the Console, select its Schedule tab, and userate(1 hour). Set its variables to {"url": "https://example.com"}. The Function now scrapes and sends the result to your receiver hourly.
Check the Function’s run history for failures. A failed scrape or receiver request fails the run; it does not report a successful database write. See Function schedules to pause or remove the schedule and Function management to inspect runs.
Option B: Connect directly from your server
Keepscrape_function.py and scrape_to_mysql.py together. Set NOTTE_API_KEY and the MySQL variables on your server. The ingest URL and token are not needed for this option.
Run python scrape_to_mysql.py, then use the SQL query above to verify the saved row. Running it again should update the row rather than create a second one.
To run hourly with Unix cron, use your actual directory and Python environment paths: