Skip to main content

Python library for scraping inside Airflow.

Project description

as-scraper

Python library for scraping inside Airflow.

Installation

The as-scraper library uses Geckodriver (Firefox) for scraping with the Selenium library. In order to use it, you need to have an airflow image having the Geckodriver dependency.

We have the as-airflow Docker image for you to have airflow ready with the Geckodriver dependency.

To use this library follow the next steps:

1. Download the docker-compose.yml file from the Airflow docs.

Airflow provides the docker-compose.yml file you need for this library.

You can directly copy the docker-compose.yml file from here or run the following command to download it:

curl -LfO 'https://airflow.apache.org/docs/apache-airflow/2.3.4/docker-compose.yaml'

2. Modify the docker-compose.yml file.

After that, simply go into the docker-compose.yml file and change the airflow image used:

...
version: '3'
x-airflow-common:
  &airflow-common
  # In order to add custom dependencies or upgrade provider packages you can use your extended image.
  # Comment the image line, place your Dockerfile in the directory where you placed the docker-compose.yaml
  # and uncomment the "build" line below, Then run `docker-compose build` to build the images.
  image: ${AIRFLOW_IMAGE_NAME:-almiavicas/as-airflow:2.2.3}
  ...

And that's it! You can now start using the as-scraper library.

Usage

If you are starting a new Airflow project, before running your containers you need to run the following command to configure volumes:

mkdir dags/ logs/ plugins/

You can now run docker-compose up and you'll have your Airflow environment up & running.

Creating a simple scraper

Lets say that we want to scrap yellowpages.com. Our target data would be the popular cities that we can find in the sitemap url.

Our output data will have two columns: name of the city and url which is linked to the city. For example, for Houston, we would want the following output:

name url
Houston https://www.yellowpages.com/houston-tx

Declaring our Scraper Class

So first we create a scraper that extends from the Scraper class, and define the COLUMNS variable to ['name', 'url'].

Create the plugins/scrapers/yellow_pages.py file and type the following code into it:

from as_scraper.base.scraper import Scraper


class YellowPagesScraper(Scraper):
    COLUMNS = ['name', 'url']

Deciding wether to load javascript or not

Now, there are two execution options when running scrapers. We can either load javascript which uses the Selenium library, or not load javascript and use the requests library for http requests.

For this example, let's go ahead and use the Selenium library. To configure this, simply add the following variable to your scraper:

from as_scraper.base.scraper import Scraper


class YellowPagesScraper(Scraper):
    COLUMNS = ['name', 'url']
    LOAD_JAVASCRIPT = True

Defining the scrape_handler

And the magic comes in the next step. We will define the scrape_handler method in our class, which will have the responsibility to scrape a given url and extract the data from it.

All scrapers must define the scrape_handler method.

from typing import Optional
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
import pandas as pd
from as_scraper.base.scraper import Scraper


class YellowPagesScraper(Scraper):
    COLUMNS = ['name', 'url']
    LOAD_JAVASCRIPT = True

    def scrape_handler(self, url: str, html: Optional[str] = None, driver: Optional[Firefox] = None, **kwargs) -> pd.DataFrame:
        rows = []
        div_tag = driver.find_element(By.CLASS_NAME, "row-content")
        div_tag = div_tag.find_element(By.CLASS_NAME, "row")
        section_tags = div_tag.find_elements(By.TAG_NAME, "section")
        for section_tag in section_tags:
            a_tags = section_tag.find_elements(By.TAG_NAME, "a")
            for a_tag in a_tags:
                city_name = a_tag.text
                city_url = a_tag.get_attribute("href")
                rows.append({"name": city_name, "url": city_url})
        df = pd.DataFrame(rows, columns=self.COLUMNS)
        return df

Creating the DAG.

Now we want to create a DAG that will trigger the scraper. For that we will use the ScraperToLogsOperator

Create the dags/yellow_pages.py file and copy the following content into it:



          

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

as-scraper-1.2.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

as_scraper-1.2.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file as-scraper-1.2.0.tar.gz.

File metadata

  • Download URL: as-scraper-1.2.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for as-scraper-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1445f31c06896ddce1e57c66df1a81d4475b37da31ce6e8afaf5eefddc47195f
MD5 2c8d78d8bdc7180179eba46f429c0c9e
BLAKE2b-256 d750d3c25c99897e232f8f22a123c05ed92ced849573e04665f26b6fc22e8690

See more details on using hashes here.

File details

Details for the file as_scraper-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: as_scraper-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for as_scraper-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b4068684c4106b48a03fa580a55d4681751cab9f26c3229a79f49fe1bfda7ac
MD5 fef91236a2b5ef9165a9132584040c0f
BLAKE2b-256 17b6250ed99ca1198deaa566a7e88819e8f4a7cf99412b33bf1725e42486bd27

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page