Volatil3
Volatil3

Reputation: 15008

Python xPath: How to get values in Iteration?

I am fetching a list from here

<div class="con">                    
                                                 <div style="float:left; width:100px; text-align:center; height:100px"><div class="photoBox" style=" overflow:hidden; width:84px; height:84px;"><a href="/b/mesa_az/1220832011"><img src="/theme/default/images/no_img/business/b__80x80"></a></div></div>
                              <div style="float:left; padding-left:10px; width:550px;">
                                <div style="float:left;"><h3><a href="/b/mesa_az/1220832011"><b>Stamps</b> Automotive Enterprises</a></h3> </div><div style="float:right;"><em>Rating:</em> <span style="color:#333; font-size:14px; font-weight:bold;">0</span> &nbsp; <img src="/theme/default/images/star/stars_00.gif" width="74" height="15"></div>

                                               <div id="clear">
                        <div style="float:left">
                        <a href="/c/mesa_az/locindustry/">Local Industry</a> / <a href="/c/mesa_az/locindustry/heavyconstruct/">Heavy Construction</a><br>
                                                                        <address>15001 S Power Rd # 1  <br>
                Mesa,  AZ 85212                   </address>
                  </div>
                                  <div style="float:right;"><h5>Click: 8 &nbsp;  Reviews: 0 </h5></div>




                               </div>
                              </div>

                    </div>

Now in Loop I have to search if there's text Cool Biz Name, get it's URL

I am trying following but it's getting all links in each iteration:

b_list = tree.xpath('//*[@class="con"]')
    for biz in b_list:
        link = biz.xpath('//h3/text()')
        print(link)

How can I let xPath to traverse with respect to DOM within biz?

Thanks

Upvotes: 0

Views: 99

Answers (1)

alecxe
alecxe

Reputation: 474141

The inner XPath has to start with a dot to be context-specific. Also, you need to use .text_content() if you want to get the text of the node children as well:

b_list = tree.xpath('//*[@class="con"]')
for biz in b_list:
    link = biz.xpath('.//h3')[0].text_content()
    print(link)

You can also use findtext() method:

biz.findtext('h3')

Upvotes: 1

Related Questions