Giter Site home page Giter Site logo

ankle's Introduction

ankle

Build Status PyPI version

ankle is a tool that finds elements inside HTML documents by comparing them with an HTML skeleton. It is useful in testing to check markup returned by a server or possibly web scraping.

Works on Python 2.7 and Python 3.4+.

Installation

$ pip install ankle

Usage

ankle.find_all(skeleton, document)

Return all elements from document that match given skeleton.

Skeleton elements are compared with the document's by tag name, attributes and text inside or between them.

Children of elements in the skeleton are looked for in the descendants of matching elements in the document.

Order of elements in the skeleton is signficant.

Skeleton must contain one root element.

document and skeleton may be either HTML strings or parsed etrees.

ankle.find(skeleton, document)

Return the first element that matches given skeleton in the document.

ankle.find_iter(skeleton, document)

Return an iterator that yields elements from the document that match given skeleton.

See find_all for details.

Caveats

  • Class attribute is checked by strict equality, but it may be desirable to ignore classes that aren't present in the skeleton
  • Text inside and between elements is checked strictly, so, for example, if it's broken up by a <span> in the document, but presented without it in the skeleton, it won't be found
  • There are no nice assertion failure messages when using in py.test so it's difficult to see what failed to match and why

Example

import ankle

document = """
<html>
	<body>
		<h1>My document</h1>
		<p>
			Some text
		</p>
		<form id="subscription-form">
			<h2>Subscribe for more information!</h2>
			<div class="control-row">
				<label for="email">Email</label>
				<input name="email" placeholder="Email"/>
			</div>
			<div class="control-row">
				<label for="tos">
					<input name="tos" type="checkbox" class="checkbox-input">
					<span class="checkbox-text">I agree to TOS</span>
				</label>
			</div>
			<div class="submit-row">
				<button type="submit">Subscribe</button>
			</div>
		</form>
	</body>
</html>
"""

skeleton = """
<form>
	Subscribe for more information!
	<label for="email">Email</label>
	<input name="email">
	<label for="tos">
		<input name="tos" type="checkbox">
		I agree to TOS
	</label>
	<button type="submit"></button>
</form>
"""

ankle.find(skeleton, document)  # will return the <form> element from the document

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.