# AutoScraper

> Learn web scraping rules from example data, no selectors required

- Category: Data & Labeling / Web Scraping & Crawling
- Language: Python
- Licence: MIT
- Repository: https://github.com/alirezamika/autoscraper
- Canonical page: https://ai-tldr.dev/tools/autoscraper/

## In short

AutoScraper is a Python library that builds web scrapers from examples instead of hand-written selectors. You give it a URL (or raw HTML) plus a short list of sample values you want from the page, and it figures out the rules that match those values. It sits in the Web Scraping & Crawling category of the Data & Labeling tools and is open-source, written in Python and licensed MIT.

## Overview

AutoScraper is a Python library that builds web scrapers from examples instead of hand-written selectors. You give it a URL (or raw HTML) plus a short list of sample values you want from the page, and it figures out the rules that match those values. It then returns the similar elements on that page.

Once a scraper is built, you can reuse the same learned object on new URLs that share the same layout, such as other product, profile, or listing pages on the same site. The wanted data can be text, a URL, or any HTML tag value, and you can save the model to a file and load it again later.

It fits the web scraping and crawling category as a fast, dependency-light option for developers who want results quickly without writing and maintaining CSS or XPath selectors. It is compatible with Python 3 and passes parameters through to the requests module, so proxies and custom headers work out of the box.

## What it does

- Learns scraping rules automatically from a list of example values you want from a page
- Works from a live URL or from raw HTML content you already have
- Reuses a built scraper on new pages with similar structure via get_result_similar and get_result_exact
- Matches text, URLs, or any HTML tag value as the wanted data
- Passes through custom requests parameters such as proxies and headers via request_args
- Saves and loads built models to and from a file for later reuse

## Getting started

Install AutoScraper from PyPI, then build a scraper by pointing it at a URL and a few sample values you want.

1. Install from PyPI

   AutoScraper is compatible with Python 3. Install the latest release with pip.

```bash
pip install autoscraper
```

2. Build a scraper from an example

   Pass a URL and a wanted_list of sample values. AutoScraper learns the rules and returns the similar elements on the page.

```python
from autoscraper import AutoScraper

url = 'https://stackoverflow.com/questions/2081586/web-scraping-with-python'

# Add one or more candidate values you want from the page
wanted_list = ["What are metaclasses in Python?"]

scraper = AutoScraper()
result = scraper.build(url, wanted_list)
print(result)
```

3. Reuse the scraper on new pages

   Apply the same learned scraper to other pages with a similar layout to pull matching content.

```python
scraper.get_result_similar('https://stackoverflow.com/questions/606191/convert-bytes-to-a-string')
```

4. Save and load the model

   Persist a built scraper to a file and load it back later instead of rebuilding it.

```python
# Give it a file path
scraper.save('yahoo-finance')

# Later
scraper.load('yahoo-finance')
```

## When to use it

- Quickly extract repeated items like post titles, prices, or links from a page without writing CSS or XPath selectors
- Build a reusable scraper for one site layout, then run it across many similar pages such as product or profile pages
- Pull a fixed set of fields (for example about text, star count, and an issues link) in a known order with get_result_exact
- Wrap a learned scraper in a small API or script to turn a website into structured data
