Hi we are running this code and want to loop over rows. I've got something completely wrong how Pythn handles the xpath selectors. It works in my Chrome xpath browsers, just not in python.
- we capture a data table in
table
this works - then we grab all underlying rows in TR
My question is: how can I grab the tbody/tr's
and color
properly and most logically? I have tried //
and ./
and /
...
For
color_rows = table.xpath('/tbody/tr')
I would expect to be able to use/tbody/tr
directly because the data is directly under thetable
. Somehow I have to use//
to get it to work, why?For
color = color_row.xpath('/td[1]/b/text()').get().strip()
I would expect to be able to use/td[1]/b/text()
directly because the data is directly under thetr
. Somehow I have to use//
to get it to work, why?table = response.xpath('//div[@class="content"]//table[contains(@class,"table")]') color_rows = table.xpath('/tbody/tr') for color_row in color_rows: color = color_row.xpath('/td[1]/b/text()').get().strip()
Our html data looks like this
<table class="table">
<thead>
<tr>
<th id="ctl00_cphCEShop_colColore" class="text-left" colspan="2">Colore</th>
<th>S</th>
<th>M</th>
<th>L</th>
</tr>
</thead>
<tbody>
<tr>
<td id="x">
<b>White</b>
<input type="hidden" name="data" value="3230/201">
</td>
<td id="avail">
Avail:
</td>
<td id="1">
<div>
<input name="cell" type="text" class="form-control">
<div class="text-center">179</div>
</div>
</td>
<td id="2">
<div>
<input name="cell" type="text" class="form-control">
<div class="text-center">360</div>
</div>
</td>
etc etc