Skip to main content

Library for generating XPath expressions as string using magic methods 🧙✨

Project description

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)

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

xpathi-1.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

xpathi-1.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file xpathi-1.0.0.tar.gz.

File metadata

  • Download URL: xpathi-1.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for xpathi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c7ff5a20387096b07747074efcdfbcd31a3baaacdedd72f28dbc4275acef6332
MD5 130aa2929a9d2be8dfdc890aabff9659
BLAKE2b-256 ca171a7eecf9050e37964c30d7f0699fe6c1a147894cd3a482bd816f509dc5b4

See more details on using hashes here.

File details

Details for the file xpathi-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: xpathi-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for xpathi-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8aad8bc847c5617b0082e2fa4d1f8da0a7ae5296259cdf5109601dc58b03f841
MD5 eb10074ebfd65b25605b07110611ffb2
BLAKE2b-256 fd590615617350961558a30c42948c7fbd691377fe3e52fbb89d15f955f4af84

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