Skip to main content

AdvancedHTMLParser

AdvancedHTMLParser is an Advanced HTML Parser (with optional indexing), writer, and formatter, and html->xhtml formtter written in python, and compatible and tested in Python 2.7 and Python 3.4.

There are many potential applications, not limited to:
  • Webpage Scraping / Data Extraction

  • Testing and Validation

  • HTML Modification/Insertion

  • Debugging

  • HTML Document generation

  • Web Crawling

  • Formatting HTML documents or web pages

Full API

Can be found Here .

Short Doc

The AdvancedHTMLParser can read in a file (or string) of HTML, and will create a modifiable DOM tree from it. It can also be constructed manually from AdvancedHTMLParser.AdvancedTag objects.

The parser then exposes many “standard” functions as you’d find on the web for accessing the data:

getElementsByTagName - Returns a list of all elements matching a tag name

getElementsByName - Returns a list of all elements with a given name attribute

getElementById - Returns a single AdvancedTag (or None) if found an element matching the provided ID

getElementsByClassName - Returns a list of all elements containing a class name

getElementsByAttr - Returns a list of all elements matching a paticular attribute/value pair.

getElementsWithAttrValues - Returns a list of all elements with a specific attribute name containing one of a list of values

getHTML - Returns string of HTML representing this DOM

getRootNodes - Get a list of nodes at root level (0)

getFormattedHTML - Returns a formatted string (using AdvancedHTMLFormatter; see below) of the HTML. Takes as argument an indent (defaults to two spaces)

The results of all of these getElement* functions are TagCollection objects. These objects can be modified, and will be reflected in the parent DOM.

Style Attribute

Style attributes can be manipulated just like in javascript, so element.style.position = ‘relative’ for setting, or element.style.position for access. There are also helper methods, getStyle(name) and setStyle(name, value) which will set the correct values.

TagCollection

A TagCollection can be used like a list. It also exposes the various get* functions which operate on the elements within the list (and their children).

AdvancedTag

the AdvancedTag represents a single tag and its inner text. It exposes many of the functions and properties you would expect to be present if using javascript. each AdvancedTag also supports the same getElementsBy* functions as the parser. It adds several additional that are not found in javascript, such as peers and arbitrary attribute searching.

some of these include:

appendText - Append text to this element

appendChild - Append a child to this element

removeChild - Removes a child

getChildren - Returns the children as a list

getStartTag - Start Tag, with attributes

getEndTag - End Tag

getPeersByName - Gets “peers” (elements with same parent, at same level in tree) with a given name

getPeersByAttr - Gets peers by an arbitrary attribute/value combination

getPeersWithAttrValues - Gets peers by an arbitrary attribute/values combination.

getPeersByClassName - Gets peers that contain a given class name

getElement* - Same as above, but act on the children of this element.

{get,set,has}Attribute - get/set/test for an attribute

{add,remove}Class - Add/remove a class from the list of classes

getUid - Get a unique ID for this tag (internal)

__str__ - str(tag) will show start tag with attributes, inner text, and end tag

__getitem__ - Can be indexed like tag[2] to access second child.

And some properties:

children/childNodes - The children as a list

innerHTML - The innerHTML including the html of all children

outerHTML - innerHTML wrapped in this tag

classNames/classList - a list of the classes

parentNode/parentElement - The parent tag

tagName - The tag name

IndexedAdvancedHTMLParser

IndexedAdvancedHTMLParser provides the ability to use indexing for faster search. If you are just parsing and not modifying, this is your best bet. If you are modifying the DOM tree, make sure you call IndexedAdvancedHTMLParser.reindex() before relying on them. Each of the get* functions above takes an additional “useIndex” function, which can also be set to False to skip index. See constructor for more information, and “Performance and Indexing” section below.

AdvancedHTMLFormatter and formatHTML

The AdvancedHTMLFormatter formats HTML into a pretty layout. It can handle elements like pre, core, script, style, etc to keep their contents preserved, but does not understand CSS rules.

The methods are:

parseStr - Parse a string of contents parseFile - Parse a filename or file object

getHTML - Get the formatted html

A script, formatHTML comes with this package and will perform formatting on an input file, and output to a file or stdout:

Usage: formatHTML [/path/to/in.html] (optional: [/path/to/output.html])

Formats HTML on input and writes to output file, or stdout if output file is omitted.

Notes

  • Each tag has a generated unique ID which is assigned at create time. The search functions use these to prevent duplicates in search results. There is a global function in the module, AdvancedHTMLParser.uniqueTags, which will filter a list of tags and remove any duplicates. TagCollections will only allow one instance of a tag (no duplicates)

  • In general, for tag names and attribute names, you should use lowercase values. During parsing, the parser will lowercase attribute names (like NAME=”Abc” becomes name=”Abc”). During searching, however, for performance reasons, it is assumed you are passing in already-lowercased strings. If you can’t trust the input to be lowercase, then it is your responsibility to call .lower() before calling .getElementsBy*

  • If you are using this to construct HTML and not search, I recommend either setting the index params to False in the constructor, or calling AdvancedHTMLParser.disableIndexing()

  • There are additional functions and usages not documented here, check the file for more information.

Performance and Indexing

Performance is very good using this class. The performance can be further enhanced via several indexing tunables:

Firstly, in the constructor of IndexedAdvancedHTMLParser and in the reindex method is a boolean to be set which determines if each field is indexed (e.x. indexIDs will make getElementByID use an index).

If an index is used, parsing time slightly goes up, but searches become O(1) (from root node, slightly less efficent from other nodes) instead of O(n) [n=num elements].

By default, IDs, Names, Tag Names, Class Names are indexed.

You can add an index for any arbitrary field (used in getElementByAttr) via IndexedAdvancedHTMLParser.addIndexOnAttribute(‘src’), for example, to index the ‘src’ attribute. This index can be removed via removeIndexOnAttribute.

Example Usage

See This Example for an example of parsing store data using this class.

Changes

See: https://raw.githubusercontent.com/kata198/AdvancedHTMLParser/master/ChangeLog

Contact Me / Support

I am available by email to provide support, answer questions, or otherwise provide assistance in using this software. Use my email kata198 at gmail.com with “AdvancedArgumentParser” in the subject line.

Unit Tests

See “tests” directory available in github. Use “runTests.py” within that directory. Tests use my GoodTests framework. It will download it to the current directory if not found in path, so you don’t need to worry that it’s a dependency.

Metadata

Release files for AdvancedHTMLParser 6.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for AdvancedHTMLParser 6.1.0
File Size Uploaded
AdvancedHTMLParser-6.1.0.tar.gz 25.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for AdvancedHTMLParser 6.1.0
File Interpreter ABI Platform
AdvancedHTMLParser-6.1.0.linux-x86_64.tar.gz Details
AdvancedHTMLParser-6.1.0-py3-none-any.whl Python 3 none any Details
AdvancedHTMLParser-6.1.0-py2-none-any.whl Python 2 none any Details

Total release size: 116.2 kB

Release files / AdvancedHTMLParser-6.1.0.tar.gz

Download URL AdvancedHTMLParser-6.1.0.tar.gz
Size 25.7 kB
Tags Source
SHA-256 checksum
How to use checksums
77860f68908eafeef4ba4ce13b93ec144b6eceff874a907b8aa4e2e9aeddb5ac
BLAKE2b-256 checksum
How to use checksums
7a0b04d6383328ec7482a6747dd95ff17bc1bbc3a79eac0ed7567944aab9f57e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / AdvancedHTMLParser-6.1.0.linux-x86_64.tar.gz

Download URL AdvancedHTMLParser-6.1.0.linux-x86_64.tar.gz
Size 36.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9987df1561c9fdfd87f9e200a34b5b2354b498f0839710f7c71903ffe1520371
BLAKE2b-256 checksum
How to use checksums
531025c2300e97aabc0b305ecc2255a4e7658c9f5313af4afd17d6737ebf0401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / AdvancedHTMLParser-6.1.0-py3-none-any.whl

Download URL AdvancedHTMLParser-6.1.0-py3-none-any.whl
Size 27.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9801b0b459c64e124ec47ef92df08e9223d392edd41396286bf1b043250927ad
BLAKE2b-256 checksum
How to use checksums
4cffc9e9dbc215fc2a14677631204e49ba5c461a765bd19dd4044da4bde95455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / AdvancedHTMLParser-6.1.0-py2-none-any.whl

Download URL AdvancedHTMLParser-6.1.0-py2-none-any.whl
Size 27.2 kB
Tags Python 2
SHA-256 checksum
How to use checksums
493d5e64a28b6bdb0a8cecb7896a4d31127541f3411cd17f650b53b32ce74326
BLAKE2b-256 checksum
How to use checksums
ea45f52eb34b24ed1fabaf98e751e80039a110aeff7d841e8b33999dbb26a760
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

9.0.2

2 release files

9.0.1

1 release file

9.0.0

1 release file

8.1.8

1 release file

8.1.7

1 release file

8.1.6

1 release file

8.1.5

1 release file

8.1.4

1 release file

8.1.3

1 release file

8.1.2

1 release file

8.1.1

1 release file

8.1.0

1 release file

8.0.1

1 release file

8.0.0

1 release file

7.4.0

1 release file

7.3.2

1 release file

7.3.1.1

1 release file

7.3.1

1 release file

7.3.0

1 release file

7.2.3

1 release file

7.2.2

1 release file

7.2.1

1 release file

7.2.0

1 release file

7.1.0

1 release file

7.0.2

1 release file

7.0.1

1 release file

7.0.0

1 release file

6.7.0

1 release file

6.6.4

1 release file

6.6.3

1 release file

6.6.2

1 release file

6.6.1

1 release file

6.6.0

1 release file

6.5.1

1 release file

6.5.0

1 release file

6.4.2

1 release file

6.4.1

1 release file

6.4.0

1 release file

6.3.0

1 release file

6.2.5

1 release file

6.2.4

1 release file

6.2.3

1 release file

6.2.2.0

1 release file

6.2.2

6.2.1

1 release file

This release

6.1.0 This release

4 release files

6.0.1

1 release file

6.0.0

1 release file

5.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page