This README provides a comprehensive guide to configuring the web scraper for extracting information from various sections of the Lisbon School of Engineering's website (https://www.isel.pt/).
- Configuration Structure
- Scrape Instruction
- Actions
- Types
- Examples
- Usage
- Available Configuration Files
The scraper configuration is a JSON file that defines a list of scraping instructions. Each instruction represents a step in the scraping process, such as navigating to a URL, selecting elements, or extracting data.
A ScrapeInstruction object represents a single step in the scraping process. It has the following properties:
action: The type of action to perform (e.g., "entry", "select", "extract")type: The type of data to extract (e.g., "item", "list", "custom", "regex")url: The URL to scrape (only for "entry" action)selector: The CSS selector to use for finding elementsattribute: The attribute to extract from the selected elementspagination: Pagination information (if applicable)key: The key to use for the extracted data in the outputformatting_begin: The beginning of a formatting string (for custom extraction)formatting_end: The end of a formatting string (for custom extraction)regex: A regular expression pattern (for regex extraction)add: A string to add to the extracted dataoutput_name: The name of the output fileoutput_dir: The directory to save the output filechildren: A list of child instructions
entry: The starting point of the scraping process, typically a URLselect: Select elements on the page using a CSS selectorextract: Extract data from the selected elementsnavigate: Follow a link and perform more tasks on the new page
item: Extract a single piece of informationlist: Extract multiple pieces of information as a listcustom: Use custom formatting to extract informationregex: Use a regular expression to extract informationregex-add-beg: Use a regular expression and add a prefix to the extracted informationadd-beg: Add a prefix to the extracted information
Here's an example configuration for extracting news article titles and URLs:
[
{
"action": "entry",
"url": "https://www.isel.pt/noticias",
"output_dir": "output",
"output_name": "news",
"children": [
{
"action": "select",
"selector": ".item-columns",
"children": [
{
"action": "extract",
"type": "item",
"selector": ".views-field-title a",
"attribute": "text",
"key": "title"
},
{
"action": "extract",
"type": "item",
"selector": ".views-field-title a",
"attribute": "href",
"key": "url"
}
]
}
]
}
]This configuration will:
- Start at the news page
- Select all news items
- For each item, extract the title and URL
- Save the results in a JSON file in the "output" directory
-
Create a JSON configuration file based on the structure described above.
-
Run the scraper script with the configuration file as an argument:
python scraper.py <config_path>Replace
<config_path>with the path to the desired JSON configuration file. -
The script will execute the scraping instructions and save the results in the specified output directory.
Remember to adjust the selectors and structure based on the specific website you're scraping.
This project is licensed under the MIT License - see the LICENSE file for details.