I have a set of static Html files which I need to parse and fetch some details from.I'm using the Python - lxml module to grab the required details.A sample from the static html file is as shown below:
<div class="top">
<a data-bind="text">abc</a>
<span data-bind="visible:hotel.marca1!='' && hotel.marca1!='logo_ha', attr:{title:hotel.textoMarca1}" title="Hotusa" style="display: none;">
</span>
<span class="marca" data-bind="visible:hotel.marca1==='' || hotel.marca1==='logo_ha'">
</span>
<span class="star sprite-disponibilidad star1" data-bind="visible:hotel.cat === '1'" style="display: none;"></span>
<span class="star sprite-disponibilidad star2" data-bind="visible:hotel.cat === '2'" style="display: none;"></span>
<span class="star sprite-disponibilidad star3" data-bind="visible:hotel.cat === '3'" style="display: none;"></span>
<span class="star sprite-disponibilidad star4" data-bind="visible:hotel.cat === '4'"></span>
<span class="star sprite-disponibilidad star5" data-bind="visible:hotel.cat === '5'" style="display: none;"></span>
<div class="adr">
<span></span>
<span class="locality" data-bind="text: hotel.pob"></span>
</div>
</div>
<div class="top">
<a data-bind="text">dfg</a>
<span data-bind="visible:hotel.marca1!='' && hotel.marca1!='logo_ha', attr:{title:hotel.textoMarca1}" title="Hotusa" style="display: none;">
</span>
<span class="marca" data-bind="visible:hotel.marca1==='' || hotel.marca1==='logo_ha'">
</span>
<span class="star sprite-disponibilidad star1" data-bind="visible:hotel.cat === '1'" style="display: none;"></span>
<span class="star sprite-disponibilidad star2" data-bind="visible:hotel.cat === '2'" style="display: none;"></span>
<span class="star sprite-disponibilidad star3" data-bind="visible:hotel.cat === '3'" style="display: none;"></span>
<span class="star sprite-disponibilidad star4" data-bind="visible:hotel.cat === '4'" style="display: none;"></span>
<span class="star sprite-disponibilidad star5" data-bind="visible:hotel.cat === '5'" style="display: none;"></span>
<div class="adr">
<span></span>
<span class="locality" data-bind="text: hotel.pob"></span>
</div>
So Here's the problem I need to get the star rating from the span class = 'star' element which is visible ; for example in the first div[@top] ,the star rating of the span that is visible is '4' while the second div[@top] doesn't have a visible span[class=star] element so it should return a star rating of '0'. However since these elements are hidden I'm having problem to fetch em and also to get the script to return '0' star ratings on div element that has all span[@class=star] 'hidden'.
This is what i have tried until now:
tree = html.fromstring(page)
for sali in tree.xpath('//div[@class="top"]'):
for x in sali.xpath('a'):
for sal in sali.xpath('span[not(contains(@style,"display:none"))]'):
print x , sal.attrib['data-bind']
But this code doesnt help me with the result I want,what mistake am I doing?
Expected Output:
abc 4
dfg 0