Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7015443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:39:10+00:00 2026-05-27T22:39:10+00:00

The html code is blind and It contains the string PRICE in html. That

  • 0

The html code is blind and It contains the string “PRICE” in html. That partial string has to be matched with html text.If the text matches(partial match) using xpath.It should return the particular html tag path.

Note: I need to automate this logic for multiple sites.I should have to use the generic rule
(For locating “Price”,Fetching Parent tag)

This is example:

html="""<div id = "price_id">
  <span id = "id1"></span>
  <div class="price_class">
   <bold>
   <strong>
   <label>PRICE:</label> 125 Rs.
   </bold>
   </strong>
   </br>
   </br>

</div>"""

I used lxml

 from lxml.html.clean import Cleaner     

 cleaner =Cleaner(page_structure=False)
 cl = cleaner.clean_html(html)
 cleaned_html = fromstring(cl)

 for element in cleaned_html:
      if element.text == 'PRICE':
          print "matched"

How it would be written using Xpath expression?

I just need to get the div class path using xpath expression.

Also The problem is if I locate the “PRICE:” string.
I should have to get the parent valid tag that is “div” with class name “price_class”.
but here i should have to skip or remove the unwanted tags like font,bold,italic…

Could you please suggest me to get the parent valid tag of the located string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:39:10+00:00Added an answer on May 27, 2026 at 10:39 pm

    You can use the ancestor axis:

    import lxml.html
    
    html = ...
    doc = lxml.html.fromstring(html)
    
    for element in doc.xpath('//label[contains(text(), "PRICE:")]/ancestor::div[@class="price_class"]'):
        print 'Found %s: %s' % (element.tag, element.text_content().strip())
    

    output:

    Found div: PRICE: 125 Rs.
    

    EDIT: More general solution for modified question:

    doc.xpath('//*[contains(text(), "PRICE:")]/\
              ancestor::*[not(self::strong|self::bold|self::italic)][1]')
    

    It will search for an element with the text “PRICE:” and then select the first ancestor skipping strong, bold, italic. You can add more tags to the exclude list.

    Instead of an exclude list, you can search for the first good ancestor (like div, ul, etc):

    doc.xpath('//*[contains(text(), "PRICE:")]/ancestor::*[self::div|self::ul][1]')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i try yo use WebMethod to return an HTML code that contains a
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
I've got HTML code that roughly looks like this: <li id=someid-11> <img src=... alt=alt
I have XML files that are representation of a portion of HTML code. Those
Has anyone had any luck model binding two or more collections using the code
I have a login form, which contains simple text box, that shows the text
html code meta tag description, it is generated dynamically and we have quotes in
I have the following html code: <h3 id=headerid><span onclick=expandCollapse('headerid')>&uArr;</span>Header title</h3> I would like to
The following html code works in Firefox, but for some reason fails in IE
I have HTML code like this : <div> <a>Link A1</a> <a>Link A2</a> <a>Link A3</a>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.