Skip to main content

Lib to extract html elements by preserving ancestors and cleaning CSS

Project description

pypi travis coveralls

Chopper is a tool to extract elements from HTML by preserving ancestors and CSS rules.

Compatible with Python >= 2.6, <= 3.4

Installation

pip install chopper

Full documentation

http://chopper.readthedocs.org/en/latest/

Quick start

from chopper.extractor import Extractor

HTML = """
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <div id="header"></div>
    <div id="main">
      <div class="iwantthis">
        HELLO WORLD
        <a href="/nope">Do not want</a>
      </div>
    </div>
    <div id="footer"></div>
  </body>
</html>
"""

CSS = """
div { border: 1px solid black; }
div#main { color: blue; }
div.iwantthis { background-color: red; }
a { color: green; }
div#footer { border-top: 2px solid red; }
"""

extractor = Extractor.keep('//div[@class="iwantthis"]').discard('//a')
html, css = extractor.extract(HTML, CSS)

The result is :

>>> html
"""
<html>
  <body>
    <div id="main">
      <div class="iwantthis">
        HELLO WORLD
      </div>
    </div>
  </body>
</html>"""

>>> css
"""
div{border:1px solid black;}
div#main{color:blue;}
div.iwantthis{background-color:red;}
"""

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

chopper-0.5.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

chopper-0.5.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file chopper-0.5.0.tar.gz.

File metadata

  • Download URL: chopper-0.5.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for chopper-0.5.0.tar.gz
Algorithm Hash digest
SHA256 43cbfab320368406d172e9fe10131560537445266a4282850870cd3b5d1009fe
MD5 5db1f56fe298ff780587843ba5a1441d
BLAKE2b-256 3d7a7799aa2df7d4cbc9d2b3410c1df0101131c4b54a9a81b8a9c7e4a9111ae1

See more details on using hashes here.

File details

Details for the file chopper-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: chopper-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for chopper-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f09b12de9574097794bb22c0f1ea34a1b8652e6983ef23acda15f3eb2c9ed8c8
MD5 f0ac2dd8d990fe5732cc50069a4ace07
BLAKE2b-256 d65d7407a27b37331fb7787168ac1f93b9a42f146378cb4a86098b9413a6c5ad

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