Skip to main content

xpath/css based scraper with pagination

Project description

Hodor PyPI

A simple html scraper with xpath or css.

Install

pip install hodorlive

Usage

As python package

WARNING: This package by default doesn't verify ssl connections. Please check the arguments to enable them.

Sample code

from hodor import Hodor
from dateutil.parser import parse


def date_convert(data):
    return parse(data)

url = 'http://www.nasdaq.com/markets/stocks/symbol-change-history.aspx'

CONFIG = {
    'old_symbol': {
        'css': '#SymbolChangeList_table tr td:nth-child(1)',
        'many': True
    },
    'new_symbol': {
        'css': '#SymbolChangeList_table tr td:nth-child(2)',
        'many': True
    },
    'effective_date': {
        'css': '#SymbolChangeList_table tr td:nth-child(3)',
        'many': True,
        'transform': date_convert
    },
    '_groups': {
        'data': '__all__',
        'ticker_changes': ['old_symbol', 'new_symbol']
    },
    '_paginate_by': {
        'xpath': '//*[@id="two_column_main_content_lb_NextPage"]/@href',
        'many': False
    }
}

h = Hodor(url=url, config=CONFIG, pagination_max_limit=5)

h.data

Sample output

{'data': [{'effective_date': datetime.datetime(2016, 11, 1, 0, 0),
           'new_symbol': 'ARNC',
           'old_symbol': 'AA'},
          {'effective_date': datetime.datetime(2016, 11, 1, 0, 0),
           'new_symbol': 'ARNC$',
           'old_symbol': 'AA$'},
          {'effective_date': datetime.datetime(2016, 8, 16, 0, 0),
           'new_symbol': 'MALN8',
           'old_symbol': 'AHUSDN2018'},
          {'effective_date': datetime.datetime(2016, 8, 16, 0, 0),
           'new_symbol': 'MALN9',
           'old_symbol': 'AHUSDN2019'},
          {'effective_date': datetime.datetime(2016, 8, 16, 0, 0),
           'new_symbol': 'MALQ6',
           'old_symbol': 'AHUSDQ2016'},
          {'effective_date': datetime.datetime(2016, 8, 16, 0, 0),
           'new_symbol': 'MALQ7',
           'old_symbol': 'AHUSDQ2017'},
          {'effective_date': datetime.datetime(2016, 8, 16, 0, 0),
           'new_symbol': 'MALQ8',
           'old_symbol': 'AHUSDQ2018'}]}

Arguments

  • ua (User-Agent)
  • proxies (check requesocks)
  • auth
  • crawl_delay (crawl delay in seconds across pagination - default: 3 seconds)
  • pagination_max_limit (max number of pages to crawl - default: 100)
  • ssl_verify (default: False)
  • robots (if set respects robots.txt - default: True)
  • reppy_capacity (robots cache LRU capacity - default: 100)
  • trim_values (if set trims output for leading and trailing whitespace - default: True)

Config parameters:

  • By default any key in the config is a rule to parse.
    • Each rule can be either a xpath or a css
    • Each rule can extract many values by default unless explicity set to False
    • Each rule can allow to transform the result with a function if provided
  • Extra parameters include grouping (_groups) and pagination (_paginate_by) which is also of the rule format.

Building & Publishing

Prerequisites

Build workflow

Run the release helper to build and publish wheels and source archives via Hatch:

./upload.sh

The script shells out to uvx hatch build followed by uvx hatch publish so that Hatch is executed in an ephemeral environment.

Publishing requirements

  1. Configure credentials in ~/.pypirc as described in the PyPI configuration specification.
  2. Use the shared PyPI/TestPyPI credentials stored in 1Password.

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

hodorlive-1.2.16.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hodorlive-1.2.16-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file hodorlive-1.2.16.tar.gz.

File metadata

  • Download URL: hodorlive-1.2.16.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for hodorlive-1.2.16.tar.gz
Algorithm Hash digest
SHA256 995d47819e9023a7b74fb53fd3791f64c6f4959319a437ab810d41642960f314
MD5 a2cc2bdc51eb46f5c005c67fc0c6e884
BLAKE2b-256 59e3381085a81b4857ff903e5cf2d2c424088eb4f1e4c592be7222dcdef1111b

See more details on using hashes here.

File details

Details for the file hodorlive-1.2.16-py3-none-any.whl.

File metadata

  • Download URL: hodorlive-1.2.16-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for hodorlive-1.2.16-py3-none-any.whl
Algorithm Hash digest
SHA256 2a5c37ca31295c275a5a55810ed742cc126a105c871a62c63f11015f135a0697
MD5 aa82a0110a268bc45ab7e996ea754a81
BLAKE2b-256 c8548ae0199518d156b50e5ff21396253831a50a8291cb7e669a594a65ffb044

See more details on using hashes here.

Supported by

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