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 6386131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:59:08+00:00 2026-05-25T02:59:08+00:00

Here is the HTML code: <div id=someid> <h2>Specific text 1</h2> <a class=hyperlinks href=link> link1

  • 0

Here is the HTML code:

<div id="someid">
    <h2>Specific text 1</h2>
    <a class="hyperlinks" href="link"> link1 inside specific text 1</a>
    <a class="hyperlinks" href="link"> link2 inside specific text 1</a>
    <a class="hyperlinks" href="link"> link3 inside specific text 1</a>

    <h2>Specific text 2</h2>
    <a class="hyperlinks" href="link"> link1 inside specific text 2</a>
    <a class="hyperlinks" href="link"> link2 inside specific text 2</a>
    <a class="hyperlinks" href="link"> link3 inside specific text 2</a>
    <a class="hyperlinks" href="link"> link4 inside specific text 2</a>

    <h2>Specific text 3</h2>
    <a class="hyperlinks" href="link"> link1 inside specific text 3</a>
    <a class="hyperlinks" href="link"> link2 inside specific text 3</a>         

</div>  

I have to distinctly find links under each “Specific text”. The problem is that if I write the following code in python:

links = root.xpath("//div[@id='someid']//a")
for link in links:
    print link.attrib['href']

It prints ALL the links irrespective of “Specific Text x”, Whereas I want something like:

print "link under Specific text:"+specific+" link:"+link.attrib['href']

Please suggest

  • 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-25T02:59:09+00:00Added an answer on May 25, 2026 at 2:59 am

    I think you will need one XPath expression for each h2 specific text.

    Given an h2 specific text, you can get its following adjacent a siblings by:

        //div[@id='someid']/h2[.='Specific text 1']
         /following-sibling::a[
          count( . | following-sibling::h2[1]/preceding-sibling::*)
          = count(following-sibling::h2[1]/preceding-sibling::*)
          and preceding-sibling::h2[1][.='Specific text 1']]
        |
        //div[@id='someid']/h2[.='Specific text 1' and not(following-sibling::h2[1])]
        /following-sibling::a"
    

    The second //h2 selection handles the case where h2 is the last one.

    The expression above just exploits the XPath 1.0 intersection formula:

    $ns1[count(.|$ns2)=count($ns2)]
    

    You can find a lot of resources about this method, lot of answers here at SO (check my answers also). I think it’s not difficult to understand how to apply this formula, what is difficult is to understand when it must be applied.

    Credits for the formul goes to @Michael Key. Just google it a bit.

    My expression has been extended with additional predicates to handle your specific case and unified (|) with additional expression to handle last h2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have html code block like this <div id=some-div> <a href=# class=link-click>some link text</a>
Here is HTML code: <div class=row> <span class=item><a href=#>A</a></span> <span class=item><a href=#>B</a></span> <span class=item><a
Here is my HTML code- <html> <head> <link type=text/css href=/static/theme.css rel=Stylesheet /> </head> <body>
Question 1 Here is the HTML code. <div class=preferredContact paragraph>ph:<span preferredcontact=40>(02) 9540 9959</span></div> I
Here is my HTML code <div class=column> <div class=open></div> <div class=close></div> <div class=content> This
first here is my html code: <div class=outter> <div class=item></div> <div class=item></div> <div class=item></div>
here is the html code : <div id=popup class=popup_block > <img src=images/PUB-histRIRE.jpg alt=popup />
Here is my HTML code <span class=xxt style=>Hide Me</span> <div><br/></div> <div></div> <span style=> <table>
I'm trying to 'hightlight' a specific div. Here is my html code <!DOCTYPE html>
Here is my html code: <div id=notes> <table class=noborder> <tr><th class=noborder><span>notes</span></th></tr> <tr><td> <p id=notes_p>

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.