Skip to main content

A simple scrapper with structured JSON support

Project description

ScrapperJson

A simple automated scrapper with support for JSON structure of scrapping rules

Installation:

pip install scrapper-json-jawron

Usage:

from scrapper-json-jawron.scrapper import Scrapper, get_rules_from_file
from dataclasses import dataclass

# example result dataclass
@dataclass
class Article:
    url: str
    title: str
    content: str = ""
        
rules = get_rules_from_file("example.json")

# create templated scrapper
scr = Scrapper(rules, Article)

# scrap entity list
result = scr.scrap_list()

# scrap entity details
entity_rules = get_rules_from_file("example_entity.json")
for entity in result:
    entity_result = scr.scrap_entity(entity_rules, entity)

JSON structure

Main options

  • type: Defines the type of scrapping data, can be either xml or html
  • pagination: Defines if the scrapping should perform pagination using a templated url from rules file, default: false
  • pagination_limit: Defines the number of pages to scrape, default: 100
  • url: Defines the url of XML feed or HTML page that will be scrapped
  • root: Defines the root element at which scrapping begins
  • entry: Defines the particular entries elements which will be scrapped to separate objects
  • elements: Defines the scrapped properties of an entity

Elements options

  • selector: Defines the CSS or XML selector for the element
  • index: Defines the index of the element to scrape, if a selector returns multiple elements
  • item_type: Defines the type of item, can be either single or list, default: single
  • attribute: Defines the attribute of element which will be scrapped, if scrapping text use text, else use the name of the attribute. You can also scrape an element as an object to use in nested properties. default: text
  • transform: Defines the cleaning and transformation options to be executed on the result property
  • prefix: Defines the prefix which is added to the result property. To be moved in next version into transform.
  • suffix: Defines the suffix which is added to the result property. To be moved in next version into transform.
  • remove: Defines the text which is to be removed from the result property. To be moved in next version into transform.
  • replace: Defines the text which is to be replaced from the result property. To be moved in next version into transform.
  • elements: Defines the scrapped properties of an entity

Cleaning and transformation options

Simple options (pass as string)

  • REMOVE_NEWLINES:
  • STRIP:
  • TO_INTEGER:
  • TO_FLOAT:
  • COLLAPSE_WHITESPACE:
  • TO_LOWERCASE:
  • TO_UPPERCASE:
  • TO_TITLECASE:
  • EXTRACT_FIRST_NUMBER:
  • STRIP_HTML_TAGS:

Advanced options (pass as a dictionary)

  • REMOVE_PREFIX:
  • REMOVE_SUFFIX:
  • TO_DATE:
  • TO_DATETIME:

Example HTML

{
  "type": "html",
  "url": "https://www.technewsworld.com/archive",
  "root": {
    "selector": "div.category-article-list",
    "attribute": "element"
  },
  "entry": {
    "selector": "div.search-item",
    "attribute": "element",
    "item_type": "list"
  },
  "elements": {
    "title": {
      "selector": "div.search-txt a h2",
      "attribute": "text",
      "transform": ["REMOVE_NEWLINES", "STRIP", "TO_TITLECASE"]
    },
    "url": {
      "selector": "div.search-txt a",
      "attribute": "href"
    }
  }
}

Example XML

{
  "type": "xml",
  "url": "https://www.cijeurope.com/rss/posts/en.xml",
  "root": "channel",
  "entry": "item",
  "elements": {
    "title": {
      "selector": "title",
      "attribute": "text"
    },
    "url": {
      "selector": "link",
      "attribute": "text"
    }
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change or add.

Plans for future

  • Pagination handling ✅
  • Network error handling and retries ✅
  • More export options (file etc.) ✅
  • Rule-based data transformation and cleaning ✅
  • Login handling, session management
  • Custom pagination functions
  • Export to database
  • Support for asynchronous requests

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

scrapper_json_jawron-0.2.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

scrapper_json_jawron-0.2.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file scrapper_json_jawron-0.2.0.tar.gz.

File metadata

  • Download URL: scrapper_json_jawron-0.2.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scrapper_json_jawron-0.2.0.tar.gz
Algorithm Hash digest
SHA256 edaefce3d627085c8ef255d7b44ef39ffa1c04765e98473a6d8e46ad17a8eb34
MD5 6803e17e9fc59250e5494a746a6bfbad
BLAKE2b-256 806ce685136f28dbe74b7f12789187c4ee4ff93e438036b91fa4d5085fff2b7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapper_json_jawron-0.2.0.tar.gz:

Publisher: python-publish.yml on TheTasak/ScrapperJson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scrapper_json_jawron-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for scrapper_json_jawron-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43b9736f3b530d43617f69c4fce1e2f41892f22f1f6d19c30835d0e9d605a4d2
MD5 e95f6142540ac9e0eff4a87a08734a37
BLAKE2b-256 eeb3e34259394533a627408fb3e94c1cbafcfca49d8d6ce139c9561dfbec08ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for scrapper_json_jawron-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on TheTasak/ScrapperJson

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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