Skip to main content

Lib to extract html elements by preserving ancestors and cleaning CSS

Project description

pypi travis coveralls

Extracts html contents by preserving ancestors and clean CSS

Compatible with Python >= 2.6, <= 3.4

Installation

pip install chopper

Usage

from chopper 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.2.4.tar.gz (8.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: chopper-0.2.4.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for chopper-0.2.4.tar.gz
Algorithm Hash digest
SHA256 9b5a4c7642d83d4d1effa1844e54fc868fa601d9766b89a78c82e7e5075f1ce9
MD5 6dcb107b6a004388390eafa499baf0d5
BLAKE2b-256 d3678befc825a16cbcce68f05aa2fa86257d8b31eca0b5e8f57ade313b1b9faa

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