Skip to main content

Python library for scraping inside Airflow.

Project description

as-scraper

PyPI - Python Version PyPI - Downloads

Python library for scraping using Selenium

Installation

The as-scraper library uses Geckodriver (Firefox) for scraping with the Selenium library. In order to use it, you need to have an Geckodriver dependency. Check the selenium documentation for details about how to install the Firefox browser driver.

Usage

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 scrapers/yellowpages.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

Execution

Finally, to execute the scraper you must call the *execute 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

if __name__ == '__main__':
    urls = ['https://www.yellowpages.com/sitemap']
    scraper = YellowPagesScraper(urls)
    results, errors = scraper.execute()
    print(results)
    print(errors)

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-2.0.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

as_scraper-2.0.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for as-scraper-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f0a8150cda63a8d5ab73cdbd3c1ad9aaa00b2da0f816c8b3dbd3520f2a8b4003
MD5 382cfc4a2428d10b2a468e5108c9fbd4
BLAKE2b-256 97a9ca545d406c620ed806b8ce79186a29114c46f840700eaa75929b18451d15

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for as_scraper-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8643d73ee2160d82dba8239a7d1497f0bbe76cc39bb1565f2feadd1893d60cf0
MD5 ac9e414d336a99ee11e7b4d835ce2ebe
BLAKE2b-256 fd7cd0a366c4256cd1d8e21f9f03a38c1313c0aa02ba04bdb8b3c9a467a6b18b

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