SIM
SIM

Reputation: 22440

How to locate the certain value of an item using xpath?

I've written a script in python in combination with lxml library using xpath to parse the value of a certain apartment from a webpage. The name of that aparment visible in that site is Eden Tolly Cascades and the value of it is 15.92 to 36.28 Lac. How can i parse the value of it mentioning that apartment name within my xpath?

I've tried like below and it is working without any issues. However, ain't there any cleaner approach to achieve the same?

import requests
from lxml.html import fromstring

url = "https://www.99acres.com/ppc-2515-residential-apartment-mailer"

res = requests.get(url)
tree = fromstring(res.text)
item = tree.xpath("//h1[contains(.,'Eden Tolly')]/../../../following::div//span/strong/text()")[0]
print(item)

This is one such container:

<div class="pro-text">
    <div class="product-text-box">
        <div class="product-heading">
            <span><img src="https://newprojects.99acres.com/projects/eden_group/eden_tolly_cascades/ln9rkpsb.jpg">
                <h1 class="font-size15">Eden Tolly Cascades<p>Kabardanga</p></h1>
            </span> 
        </div>
    </div>
    <div class="product-text-box">
    <ul class="product-lrg-box">
        <li> <span><strong><span class="rupee-font">₹ &nbsp;</span>15.92 to 36.28 Lac</strong></span></li>
        <li><strong>549-1251 SQFT</strong></li>

        <li><strong>1-3 BHK</strong></li>
        <li style="width:20% !important;"><strong>New Launch</strong></li>
    </ul>
    <div id="tabs" class="tab-link tabs-menu tabs-menu-new">
        <ul>
            <li><a href="#304355broch">e-Brochure</a></li>
            <li><a href="#304355amn">Amenities</a></li>
            <!--  <li style="width:20% !important;"><a href="#304355floor">Floor Plan</a></li>-->
            <li style="width:20% !important;"><a href="#304355dir">Directions</a></li>
        </ul>
    </div>
    <span class="enquire-new-bt" id="304355-583061,151100-enquire-new-bt" data-val="3"> <a href="javascript:void(0)">I am Interested</a> </span> </div>
</div>

Upvotes: 1

Views: 65

Answers (1)

Andersson
Andersson

Reputation: 52675

Try below XPath

//div[contains(.//h1, 'Eden Tolly')]/following-sibling::div//span/strong/text()

Upvotes: 1

Related Questions