A template example built with Selenium and headless Chrome browser to scrape a website and save the results to storage. The URL of the web page is passed in via input, which is defined by the input schema. The template uses the Selenium WebDriver to load and process the page. Enqueued URLs are stored in the default request queue. The data are then stored in the default dataset where you can easily access them.
- Apify SDK - toolkit for building Apify Actors
- Input schema - define and easily validate a schema for your Actor's input
- Request queue - queues into which you can put the URLs you want to scrape
- Dataset - store structured data where each object stored has the same attributes
This code is a Python script that uses Selenium to scrape web pages and extract data from them. Here's a brief overview of how it works:
- The script reads the input data from the Actor instance, which is expected to contain a
start_urls
key with a list of URLs to scrape. - The script processes the requests in the queue one by one, fetching the URL using requests and parsing it using Selenium.
- The script extracts the desired data from the page (in this case, titles of each page) and pushes them to the default dataset using the
push_data
method of the Actor instance. - The script catches any exceptions that occur during the scraping process and logs an error message using the
Actor.log.exception
method.
For complete information see this article. To run the actor use the following command:
apify run
If you've created a Git repository for the project, you can easily connect to Apify:
- Go to Actor creation page
- Click on Link Git Repository button
You can also deploy the project on your local machine to Apify without the need for the Git repository.
-
Log in to Apify. You will need to provide your Apify API Token to complete this action.
apify login
-
Deploy your Actor. This command will deploy and build the Actor on the Apify Platform. You can find your newly created Actor under Actors -> My Actors.
apify push
To learn more about Apify and Actors, take a look at the following resources: