اذهب إلى المحتوى
  • 1

كيف أستعمل regular expressions داخل Python لـ XPath؟

K@m@l

السؤال

على لغة البرمجة Python، حاولت استعمال regular expressions:

for element in root.xpath('//a[@id="hypProduct_[0-9]+"]'):

كيف أستعمل +[9-0] مع XPath لتحديد عناصر lXml:

By default, XPath supports regular expressions in the EXSLT namespace:

>>> regexpNS = "http://exslt.org/regular-expressions"
>>> find = etree.XPath("//*[re:test(., '^abc$', 'i')]",
...                    namespaces={'re':regexpNS})

>>> root = etree.XML("<root><a>aB</a><b>aBc</b></root>")
>>> print(find(root)[0].text)
aBc

ما الحل؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

في حالتك، سنستعمل:

a[re:test(@id, "^hypProduct_[0-9]+$")]

وهذا مثال تطبيقي:

>>> from lxml.html import fromstring
>>> 
>>> data = '<a id="hypProduct_10">link1</a>'
>>> tree = fromstring(data)
>>> tree.xpath('//a[re:test(@id, "^hypProduct_[0-9]+$")]', namespaces={'re': "http://exslt.org/regular-expressions"})[0].attrib["id"]
'hypProduct_10'

مصادر:

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...