A flexible web scraping API that uses AI to extract structured data from any webpage.
- Clone the repository:
git clone https://github.com/trchristensen/aiscraper.git && cd aiscraper
- Install dependencies:
composer install
- Set up your environment:
cp .env.example .env
# Edit .env with your configuration
- Configure your OpenAI API key in
.env
:
OPENAI_API_KEY=your-openai-api-key-here
- Set up your web server (Apache/Nginx) to point to the project directory
Make a POST request to /api/retrieve/
with the following JSON body:
{
"api_key": "test-key-123",
"webpage_url": "https://example.com/product",
"api_method_name": "getItemDetails",
"api_response_structure": {
"item_name": "<the item name>",
"item_price": "<the item price>",
"item_image": "<the absolute URL of the first item image>"
}
}
api_key
: Your API authentication key (default test key: "test-key-123")webpage_url
: The URL of the webpage to scrapeapi_method_name
: Name of the extraction method (used for AI context)api_response_structure
: JSON object defining the structure of data to extractverbose
(optional): Set totrue
to get additional debug information
{
"response": {
"item_name": "Example Product",
"item_price": "$99.99",
"item_image": "https://example.com/image.jpg"
}
}
{
"error": true,
"reason": "Error message here"
}
- Generic web scraping - works with any webpage
- AI-powered extraction using OpenAI's GPT models
- Flexible response structure
- Built-in HTML cleaning and preprocessing
- Error handling and logging
- Rate limit handling
- Verbose mode for debugging
- PHP 7.4 or higher
- OpenAI API key
- cURL extension enabled
- Web server (Apache/Nginx)
- Store your OpenAI API key securely (preferably in environment variables)
- Implement proper API key validation for production use
- Consider implementing rate limiting for your API endpoints
- Monitor your OpenAI API usage and costs
This project is licensed under the MIT License - see below for details:
MIT License
Copyright (c) 2024 Todd Christensen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.