Skip to main content

xpathi

It is a library for generating XPath expressions as string 🧙✨

Reasons to use xpathi:

  • Code expressions look like real xpath expressions

  • Returns string type, you needn’t convert it

  • Implements many xpath features

  • Dont have any dependencies

  • Easy to extend, you can add custom methods and attributes in one line

Installation

pip install xpathi

Generating expressions

Easily create xpath expression using magic methods:

from xpathi.elements import *

xpath = div // a(text='link') / _href
print(type(xpath))
# <class 'str'>
print(xpath)
# .//div//a[text()="link"]/@href

xpath = div / div(_class='wrapper')[last() - 1] // a(contains(_href, 'google'), text='click') / _href
# .//div/div[@class="wrapper"][last()-1]//a[contains(@href, "google") and text()="click"]/@href

Result of expressions is always a valid xpath with string type. You can pass it like selector.xpath(div // a)

Specify tag like tag1 / tag2

Specify attribute like tag / _attribute (prefer this method) or tag.attribute

Specify parameters like tag(_attr1=val1, _attr2=val2, text=val3)

Specify order like tag[0], tag[last()], tag[last()-1], tag[-2]

Usage examples

Description

Code

Result

Single slash

div / a

.//div/a

Double slash

div // a

.//div//a

Parameters

div // a(_class='c1', _maxlength=10)

.//div//a[@class=”c1” and @maxlength=10]

Parameters text()

div / a(text='short', _class='c1')

.//div/a[text()=”short” and @class=”c1”]

Get attribute

div / a / _href

.//div/a/@href

Get attribute

div / a.href

.//div/a/@href

Get text attribute

div / a / text()

.//div/a/text()

Index

div / a[3] / p

.//div/a[3]/p

Index using last

div / a[last() - 1] / _href

.//div/a[last()-1]/@href

Index negative

div / a[-2] / _href

.//div/a[last()-1]/@href

Contains

div / a(contains(_href, 'https'), _class='c1')

.//div/a[contains(@href, “https”) and @class=”c1”]

Contains

div / a(contains('@href', 'https'), _class='c1')

.//div/a[contains(@href, “https”) and @class=”c1”]

Contains text()

div / a(contains(text(), 'https'), _class='c1')

.//div/a[contains(text(), “https”) and @class=”c1”]

Any tag (*)

div / any / a

.//div/*/a

See more examples in tests

If you use only one tag (without slash), than you should call .execute() method to convert it to string:

xpath = div(_class='wrapper').execute()
print(xpath)
# div[@class="wrapper"]

Custom tags and attributes

You can easy create custom tags, attributes and functions:

from xpathi.base import Element  # to create customs
from xpathi.elements import *  # to build xpath

tag = Element('tag')
_attribute = Element('@attribute')
function = Element('function()')

xpath = tag / _attribute / function
print(xpath)
# .//tag/@attribute/function()

xpath = tag(_attribute='val1', function='val2') // _attribute
print(xpath)
# .//tag[@attribute="val1" and function()="val2"]//@attribute

More

PyPI: https://pypi.org/project/xpathi

Repository: https://github.com/abionics/xpathi

Developer: Alex Ermolaev (Abionics)

Email: abionics.dev@gmail.com

License: MIT (see LICENSE.txt)

Metadata

Release files for xpathi 1.0.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 xpathi 1.0.0
File Size Uploaded Provenance Actions
xpathi-1.0.0.tar.gz 8.4 kB Without provenance Details for xpathi-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xpathi 1.0.0
File Interpreter ABI Platform Actions
xpathi-1.0.0-py3-none-any.whl Python 3 none any Details for xpathi-1.0.0-py3-none-any.whl

Total release size: 17.7 kB

Release files / xpathi-1.0.0.tar.gz

Download URL xpathi-1.0.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c7ff5a20387096b07747074efcdfbcd31a3baaacdedd72f28dbc4275acef6332
BLAKE2b-256 checksum
How to use checksums
ca171a7eecf9050e37964c30d7f0699fe6c1a147894cd3a482bd816f509dc5b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

Release files / xpathi-1.0.0-py3-none-any.whl

Download URL xpathi-1.0.0-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8aad8bc847c5617b0082e2fa4d1f8da0a7ae5296259cdf5109601dc58b03f841
BLAKE2b-256 checksum
How to use checksums
fd590615617350961558a30c42948c7fbd691377fe3e52fbb89d15f955f4af84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

Security for xpathi 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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