Skip to main content

Lib to extract html elements by preserving ancestors and cleaning CSS

Project description

https://travis-ci.org/jurismarches/tree_extractor.svg?branch=master

Extracts html contents by preserving ancestors and clean CSS

Compatible with Python >= 2.6, <= 3.4

Installation

pip install tree-extractor

Usage

from tree_extractor import TreeExtractor

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 = TreeExtractor().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

tree_extractor-0.1.9.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file tree_extractor-0.1.9.tar.gz.

File metadata

File hashes

Hashes for tree_extractor-0.1.9.tar.gz
Algorithm Hash digest
SHA256 c40a4344a924ecd2f83dac46d0c82b37cbf4db074cbf885e90bd5467d5bcfcdf
MD5 5e4d1bcc518ac067210670883eafccbc
BLAKE2b-256 7d72cb4049cb0f522166df2116f614738c2a3d2a8de278cbd42fdbad3565c463

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