Issue
I've being trying to get the values of a preceding tag. Here's what I'm doing:
The struct of html page:
...
<tr class="destaque no-hover">
<td class="periodo" colspan="6">2020.1</td>
</tr>
<tr class="linhaPar">
<td>Text1</td>
<td align="center">01</td>
<td align="right">312h</td>
<td align="center">3T12</td>
</tr>
<tr class="linhaImpar">
<td>Text2</td>
<td align="center">01</td>
<td align="right">12h</td>
<td align="center">5M12</td>
</tr>
...
<tr class="destaque no-hover">
<td class="periodo" colspan="6">2016.1</td>
</tr>
<tr class="linhaPar">
<td>Text7</td>
<td align="center">01</td>
<td align="right">2h</td>
<td align="center">2N12</td>
</tr>
<tr class="linhaImpar">
<td>Text8</td>
<td align="center">01</td>
<td align="right">32h</td>
<td align="center">4T12</td>
</tr>
...
<tr class="destaque no-hover">
<td class="periodo" colspan="6">2014.2</td>
</tr>
<tr class="linhaPar">
<td>TextN-1</td>
<td align="center">01</td>
<td align="right">2h</td>
<td align="center">2N12</td>
</tr>
<tr class="linhaImpar">
<td>TextN</td>
<td align="center">01</td>
<td align="right">32h</td>
<td align="center">4T12</td>
</tr>
So, I'm trying to get the infos of each one of those tr classes="linhaPar|linhaImpar"
for i in response.xpath('//tr[@class="linhaPar" or @class="linhaImpar"]')
_aux = i.xpath('./td[1]')
However, I also need those td[@class="periodo"]
so, I'm stucked with xpath
# I've tried this, but return a list of elements that matches, not the close one, as I want
_p = _aux.xpath('./preceding::tr[td[@class="periodo"]')
# I've also tried this, but won't work
_p = _aux.xpath('./preceding::tr[td[@class="periodo"] and position()=1]')
Solved
Maybe when I was making this question I wasn't clearly enough. The periodo
change in different amounts of tr placed together. Every way that I've tried to search for, return me a list of possible results or nada. To solve the problem, I've tried the solution proposed to consider the periodo
in the "for loop xpath":
_p = ""
for i in response.xpath('//tr[@class="linhaPar" or @class="linhaImpar" or @class="destaque no-hover"]'):
# Check if it's a td with period
if 'destaque no-hover' == i.xpath('./@class').get():
_p = i.xpath('./td/text()').get()
continue # Force to go to the next one
Solution
This XPath:
'//tr[@class="linhaPar" or @class="linhaImpar" or td[@class="periodo"]]'
Answered By - Gilles Quenot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.