Python Forum

Full Version: Python - Scrapy - Contains
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Lets say we have this html, and we wanted to say if the tr in td contains EAN:, Print me the 2nd td in the same element

response.xpath('//strong[contains( text(), "EAN:")]/text()').extract()
<tr class="product-model">
   <td><strong>EAN:</strong></td>
   <td> 4048961089302</td>
</tr>

<tr class="product-model">
   <td><strong>Product.Nr.:</strong></td>
   <td> 83003</td>
</tr>
thank you :D
You can use the following-sibling axis to do this:
>>> sel = scrapy.Selector(text='''<tr class="product-model">
...    <td><strong>EAN:</strong></td>
...    <td> 4048961089302</td>
... </tr>
...
... <tr class="product-model">
...    <td><strong>Product.Nr.:</strong></td>
...    <td> 83003</td>
... </tr>''')
>>> sel.xpath('//td[contains(., "EAN:")]/following-sibling::td[1]/text()').get()
' 4048961089302'
dude....you are awesome....you have always to give the best possible answers to people.
Yeah, I'm pretty amazing.